Split a circular linked list into two halves

June 23, 2022 · 1 min read

https://practice.geeksforgeeks.org/problems/split-a-circular-linked-list-into-two-halves/1

void splitList(Node *head, Node **head1_ref, Node **head2_ref)
{
    if (head == nullptr) return;

    Node *fast = head, *slow = head;

    while (fast->next != head && fast->next->next != head) {
        slow = slow->next;
        fast = fast->next->next;
    }

    if (fast->next->next == head) {
        fast = fast->next;
    }

    *head1_ref = head;

    if (head->next != head) {
        *head2_ref = slow->next;
    }

    fast->next = slow->next;
    slow->next = head;
}
Check if a linked list is a circular linked list
Check whether the singly linked list is a palindrome