-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOddEvenLL.java
More file actions
36 lines (32 loc) · 1.34 KB
/
OddEvenLL.java
File metadata and controls
36 lines (32 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode oddEvenList(ListNode head) {
// Edge case: if the list is empty or has only one node
if (head == null || head.next == null) {
return head;
}
// Initialize pointers for odd and even nodes
ListNode odd = head; // Pointer for odd indexed nodes
ListNode even = head.next; // Pointer for even indexed nodes
ListNode evenHead = even; // Keep track of the start of the even list
// Loop until we reach the end of the list
while (even != null && even.next != null) {
odd.next = even.next; // Link current odd node to the next odd node
odd = odd.next; // Move to the next odd node
even.next = odd.next; // Link current even node to the next even node
even = even.next; // Move to the next even node
}
// Connect the end of the odd list to the head of the even list
odd.next = evenHead;
return head;
}
}