LeetCode: Odd Even Linked List Solution
2 pointers running simultaneouslyApproach
2 pointers: head for odd and head for even (in terms of original index)
Implementation
1var oddEvenList = function (head) {2 if (!head || !head.next) {3 return head4 }56 let oddHead = (oddTail = head)7 let evenHead = (evenTail = head.next)89 while (oddTail && oddTail.next && evenTail && evenTail.next) {10 oddTail.next = oddTail.next.next11 oddTail = oddTail.next1213 evenTail.next = evenTail.next.next14 evenTail = evenTail.next15 }1617 oddTail.next = evenHead1819 return oddHead20}
References
Similar problems
Comments
Loading comments...
Tags
leetcode
linked list
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.