Skip to content

Commit

Permalink
Create 143-Reorder-List.js
Browse files Browse the repository at this point in the history
  • Loading branch information
veerbia authored Apr 3, 2022
1 parent a09d41d commit 0a32dba
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions javascript/143-Reorder-List.js
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;
}


};

0 comments on commit 0a32dba

Please sign in to comment.