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:
Given 1->2->3->4->5->NULL
,
return 1->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 ...
Credits:
Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
Intuition
\nPut the odd nodes in a linked list and the even nodes in another. Then link the evenList to the tail of the oddList.
\nAlgorithm
\nThe solution is very intuitive. But it is not trivial to write a concise and bug-free code.
\nA well-formed LinkedList
need two pointers head and tail to support operations at both ends. The variables head
and odd
are the head pointer and tail pointer of one LinkedList
we call oddList; the variables evenHead
and even
are the head pointer and tail pointer of another LinkedList
we call evenList. The algorithm traverses the original LinkedList and put the odd nodes into the oddList and the even nodes into the evenList. To traverse a LinkedList we need at least one pointer as an iterator for the current node. But here the pointers odd
and even
not only serve as the tail pointers but also act as the iterators of the original list.
The best way of solving any linked list problem is to visualize it either in your mind or on a piece of paper. An illustration of our algorithm is following:
\nFigure 1. Step by step example of the odd and even linked list.
\n\nComplexity Analysis
\nTime complexity : . There are total nodes and we visit each node once.
\nSpace complexity : . All we need is the four pointers.
\n