143. Reorder List
Aug 4, 2016
https://leetcode.com/problems/reorder-list/
solution:
|
|
3 steps:
- find the middle point which seperates the list into two parts
- reverse the 2nd part of list
- “stich” two parts of list together one by one.
What I did wrong: (After thought)
I can’t find the ending condition for the while
loop below, since we are stiching the two list together, we need to find the list which is shorter, in other word: which exhaust first. Otherwise, null pointer
everywhere.12345678while (sndHead != null) { ListNode headNext = head.next; head.next = sndHead; ListNode sndHeadNext = sndHead.next; sndHead.next = headNext; head = headNext; sndHead = sndHeadNext; }