linkedlist reorder
by Krishna Ananthi
HTML
// go to mid
// reverse 2nd list
//merge two list
//make last pointer to null
JavaScript
function reorderList(head) {
console.log(head.val)
let slow = head,
fast = head
/*while (fast && fast.next) {
fast = fast.next.next
slow = slow.next
} */
console.log(slow.val,fast.val)
/* let prev = null
while (slow) {
let temp = slow.next
slow.next = prev
prev = slow
slow = temp
}
let dummy = { next: null, val: 0 }
let temp = dummy
let l1 = head
while (l1 && prev) {
if (l1.val < prev.val) {
temp.next = l1
l1 = l1.next
} else {
temp.next = prev
prev = prev.next
}
}
if (l1) temp.next = l1
else temp.next = prev
return dummy.next*/
}
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
let l4= ListNode(4,null)
let l3= ListNode(3,null)
let l2= ListNode(2, null)
let l1= ListNode(1, null)
l1.next = l2;
l2.next= l3; l3.next = l4;
console.log(reorderList(l1))