[Java][LetCode][LinkedList] Odd Even Linked List #328
head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
The first node is considered odd, and the second node is even, and so on.
Note that the relative order inside both the even and odd groups should remain as it was in the input.
You must solve the problem in
O(1) extra space complexity and
O(n) time complexity.
Input: head = [1,2,3,4,5]
My approach is :
create two list: odd and even list. Initiation the odd list from head, and initiation even list is from head.next. Alternate the odd list and the even list each other. Until, the even list is out of bound. At last, add the even list after the odd list.