C++ one-pass solution.


  • 1
    C
    void reorderList(ListNode* head) {
        if (!head || !(head->next))
            return;
        ListNode *fast = head->next, *slow = head;
        while(fast && fast->next) {
            fast = fast->next->next;
            slow = slow->next;
        }
        ListNode *cur = slow->next, *node = NULL;
        slow->next = NULL;
        while (cur) {
            ListNode *nxt = cur->next;
            cur->next = node;
            node = cur;
            cur = nxt;
        }
        while (node) {
            ListNode *nxt1 = head->next, *nxt2 = node->next;
            head->next = node;
            node->next = nxt1;
            head = nxt1;
            node = nxt2;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.