Skip to content

Commit

Permalink
Merge pull request neetcode-gh#21 from bryanhitc/brhitchc/cpp-143-reo…
Browse files Browse the repository at this point in the history
…rder-list

[C++] 143: Reorder List
  • Loading branch information
neetcode-gh authored Apr 4, 2022
2 parents a5a962f + 9f031be commit 933ca4a
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions cpp/143-Reorder-List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
void reorderList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* slow = head;
ListNode* fast = head;

// set slow to middle node
while (fast != nullptr && fast->next != nullptr) {
prev = slow;
slow = slow->next;
fast = fast->next->next;
}

if (prev != nullptr) {
prev->next = reverseList(slow);
mergeTwoLists(head, prev->next);
}
}

private:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;

while (curr != nullptr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}

return prev;
}

// invariant: list2 length = [list1 length, list1 length + 1]
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
ListNode dummy;
ListNode* prev = &dummy;

ListNode* l1 = list1;
ListNode* l2 = list2;

while (l1 != list2) {
takeNode(prev, l1);
takeNode(prev, l2);
}

// handle odd number of nodes
if (l2 != nullptr) {
takeNode(prev, l2);
}

return dummy.next;
}

void takeNode(ListNode*& prev, ListNode*& curr) {
prev->next = curr;
prev = prev->next;
curr = curr->next;
}
};

0 comments on commit 933ca4a

Please sign in to comment.