• 42.5%

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.

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 …

https://discuss.leetcode.com/topic/34837/simple-c-solution-o-n-time-o-1-space

Simple C++ solution, O(n) time, O(1) space

https://discuss.leetcode.com/topic/34473/my-c-solution

My c++ solution

https://discuss.leetcode.com/topic/34309/clear-python-solution

Clear Python Solution

https://discuss.leetcode.com/topic/36552/python-solution-with-two-pointers-o-n

Python solution with two pointers O(N)

read in two node at a time:

first node(odd) goes into odd.next

2nd node(even).next = next even node (node.next.next)

rinse and repeat

so basically

1 - 2 - 3 - 4- 5- 6 -7-null

odd = 1 even = 2

temp = 3

even(2).next = even.next.next(4)

temp(3).next=odd(1).next(2)

(this makes sure the end of odd always points to start of even)

odd(1).next = temp(3)

odd = odd.next(3) move the pointer

even = even.next(4) move the pointer

1-3(odd)-2-4(even)-5-null

1-3-5(odd)-2-4-null(even)

1-3-5-7(odd)-2-4-6-null(even)