Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example:
Given1->2->3->4->5->NULL,
return1->3->5->2->4->NULL.
Note:
The relative order inside both the even and odd groups should remain as it was in the input.
The first node is considered odd, the second node even and so on ...
這道題雖然是Medium Level, 但是還是挺簡單的。
一開始做可能會有一個誤區, 先把Even node整一組, odd node整一組。 但是這么實踐一下,會發現本來的索引會被改變。 例如 1-->2-->3-->4-->5. ? ?1本來要靠2的索引找到3,但是現在2的索引變成了4,那1就到不了3了!?
List類問題做的時候最好畫一個Simple case: 例如1->2->3->4
很容易就會發現上述問題,所以解法應該是一個的while loop,?
一遍更改even, odd nodes的索引。