Problem
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.
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.
Analysis
This problem can be solved by using two pointers. We iterate over the link and move the two pointers.
Java Solution
public ListNode oddEvenList(ListNode head) { if(head == null) return head; ListNode result = head; ListNode p1 = head; ListNode p2 = head.next; ListNode connectNode = head.next; while(p1 != null && p2 != null){ ListNode t = p2.next; if(t == null) break; p1.next = p2.next; p1 = p1.next; p2.next = p1.next; p2 = p2.next; } p1.next = connectNode; return result; } |
check this implementation for more detailed explanation
https://code4run.com/rearrange-the-linked-list-from-the-odd-and-even-nodes/
public Node apply(LinkedList list) {
Node odd = list.getHead();
Node even = list.getHead().getNext();
Node evenHead = even;
Node oddHead = odd;
while(odd != null && odd.getNext() != null && odd.getNext().getNext() != null) {
odd.setNext(even.getNext());
even.setNext(odd.getNext().getNext());
odd = odd.getNext();
even = even.getNext();
}
odd.setNext(evenHead);
return oddHead;
}
It can also be done like this, right? Slow starts from head and fast starts from head.next. They both start skipping one node and continue to parse the list. Correct me if I am wrong!
public static ListNode oddEvenList(ListNode head) {
if(head == null)
return head;
ListNode result = head;
ListNode slow=head;
ListNode fast=head.next;
ListNode temp = head.next;
while(slow!=null && fast!=null){
slow.next =slow.next.next;
slow=slow.next;
fast.next=fast.next.next;
fast=fast.next;
}
slow.next=temp;
return result;
}
I know my code has little bit complexity. But I feel like wanna post my code. I paid a lot of time to get the correct output.
Node pre=head;
Node cur=head.next;
Node later=cur.next;
while(later!=null){
Node temp=pre.next;
Node temp1=later.next;
pre.next=later;
later.next=temp;
cur.next=temp1;
pre=pre.next;
cur=cur.next;
later=cur.next;
My solution ollows the same principle as the other solutions.
{{
public class Solution {
public ListNode oddEvenList(ListNode head) {
if(head == null){return head;}
ListNode odd = head;
ListNode even = head.next;
ListNode evenStart = even;
while(even != null && even.next != null)
{
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenStart;
return head;
}
}}
Hi Admin . I just wanted a moment to thank you for providing short understandable solutions for problems here , It has been a great help 🙂
Could just have the following code, and not need Node t:
while (p1 != null && p2 != null && p2.next != null) {
p1.next = p2.next;// Connect to next odd node
p1 = p1.next;// Move odd pointer
p2.next = p1.next;// Connect to next even node
p2 = p2.next;// Move even pointer
} // end while
p1.next = connectNode;// Connect evens to odds
return result;
}// end oddEvenList