Odd Even Linked List LeetCode solution in Java

1
class Solution {
2
public ListNode oddEvenList(ListNode head) {
3
if (head == null) {
4
return head;
5
}
6
ListNode result = head, evenHalf = new ListNode(0), evenHalfPtr = evenHalf;
7
for (; head.next != null; head = head.next) {
8
evenHalfPtr = evenHalfPtr.next = head.next;
9
head.next = head.next.next;
10
evenHalfPtr.next = null;
11
if (head.next == null) {
12
break;
13
}
14
}
15
head.next = evenHalf.next;
16
return result;
17
}
18
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0