-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOddEvenLinkedList.java
More file actions
58 lines (48 loc) · 2.04 KB
/
OddEvenLinkedList.java
File metadata and controls
58 lines (48 loc) · 2.04 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package Algorithms.LinkedListAlgos;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 22 April 2025
*/
public class OddEvenLinkedList {
static class ListNode {int val; ListNode next; ListNode(int x) { val = x; } ListNode(int x, ListNode next) { val = x; this.next = next; } }
public static void main(String[] args) {
System.out.println("oddEvenList: ");
ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
oddEvenList(head);
for (ListNode node = head; node != null; node = node.next) System.out.print(node.val + " ");
System.out.println("\noddEvenListMyApproach: ");
head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
oddEvenListMyApproach(head);
for (ListNode node = head; node != null; node = node.next) System.out.print(node.val + " ");
// GIVEN: [1, 2, 3, 4, 5]
// RETURN: [1, 3, 5, 2, 4]
}
public static ListNode oddEvenList(ListNode head) {
if (head == null) return null;
ListNode odd = head, even = head.next, evenHead = even;
while (even != null && even.next != null) { // or while (odd.next != null && odd.next.next != null)
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
public static ListNode oddEvenListMyApproach(ListNode head) {
if(head == null) return head; // or if(head == null || head.next==null || head.next.next==null) return head;
ListNode odd = head, even = odd.next, evenHead = even;
while(odd.next!=null && odd.next.next!=null) {
// even temp var
even = odd.next;
// update odd using even temp var
odd.next = even.next;
// increment odd
odd = odd.next;
// update even
even.next = odd.next;
}
odd.next = evenHead;
return head;
}
}