Odd Even Linked List
Input: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULLInput: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->4->NULLInput: 1->2->3->4->5->NULL
Output: 1->3->5->2->4->NULLInput: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->4->NULLclass Solution {
public ListNode oddEvenList(ListNode head) {
ListNode dummy1 = new ListNode(0);
ListNode dummy2 = new ListNode(0);
dummy2.next = head;
ListNode i = dummy1, j = dummy2;
while (j != null && j.next != null) {
i.next = j.next;
i = i.next;
j.next = j.next.next;
j = j.next;
i.next = null;
}
i.next = dummy2.next;
return dummy1.next;
}
}