分割链表

来源:互联网 发布:网络作家跳舞 编辑:程序博客网 时间:2024/05/17 08:21

给定一个单向链表,要求从中间分成两个链表。如果长度是奇数,则左链表比右链表多一个节点。


void SplitList(Node *head, Node **left, Node **right){    if (head == NULL)    {        return;    }    Node *slow = head;    Node *fast = head;    Node *last_of_left = head;    while (fast != NULL)    {        last_of_left = slow;        slow = slow->next;        fast = (fast->next)? fast->next->next : NULL;    }    last_of_left->next = NULL;    *left = head;    *right = slow;}


0 0
原创粉丝点击