Skip to content

Commit

Permalink
Create 143.Reorder-List_v2.cpp
Browse files Browse the repository at this point in the history
1. Use fast and slow pointers;
2. More elegant revert and insertion.
  • Loading branch information
Bill0412 authored Dec 6, 2021
1 parent 7b34e95 commit fd89446
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Linked_List/143.Reorder-List/143.Reorder-List_v2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* 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 *slow = head, *fast = head;
for(; fast && fast->next; slow = slow->next, fast = fast->next->next);
ListNode *head1 = head;
ListNode *head2 = slow->next;
slow->next = nullptr;

// reverse list2
ListNode *nextNode = nullptr;
for(ListNode *cur = head2; cur != nullptr;) {
ListNode *curNext = cur->next;
cur->next = nextNode;
nextNode = cur;
cur = curNext;
}
head2 = nextNode;

// traverse list1 to insert
for(ListNode *ptr1 = head1, *ptr2 = head2; ptr1 && ptr2;) {
ListNode *ptr1Next = ptr1->next, *ptr2Next = ptr2->next;
ptr1->next = ptr2;
ptr2->next = ptr1Next;
ptr1 = ptr1Next;
ptr2 = ptr2Next;
}
}
};

0 comments on commit fd89446

Please sign in to comment.