forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* function ListNode(val, next) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.next = (next===undefined ? null : next) | ||
* } | ||
*/ | ||
/** | ||
* @param {ListNode} head | ||
* @return {void} Do not return anything, modify head in-place instead. | ||
*/ | ||
var reorderList = function(head) { | ||
if (!head) { return }; | ||
|
||
let slow = head; | ||
let fast = head; | ||
|
||
|
||
// finding the middle of the linked list using 2 pters | ||
while (fast && fast.next) { | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
} | ||
|
||
// reverse the second part of the list starting at slow | ||
let prev = null | ||
let curr = slow; | ||
while (curr) { | ||
let next = curr.next; | ||
curr.next = prev; | ||
prev = curr; | ||
curr = next; | ||
} // here prev is the head | ||
|
||
// merge two sorted lists (first one starts at head, second at prev) | ||
let first = head; | ||
let second = prev; | ||
|
||
while(second.next) { | ||
temp = first.next; | ||
first.next = second; | ||
first = temp; | ||
|
||
temp = second.next; | ||
second.next = first; | ||
second = temp; | ||
} | ||
|
||
|
||
}; |