分割链表

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


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;
}


你可能感兴趣的:(分割链表)