Skip to content

Commit 566c0aa

Browse files
authored
Day 16 solution
1 parent e30098a commit 566c0aa

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,38 @@
1+
// https://leetcode.com/problems/odd-even-linked-list/
2+
/**
3+
* Definition for singly-linked list.
4+
* public class ListNode {
5+
* int val;
6+
* ListNode next;
7+
* ListNode() {}
8+
* ListNode(int val) { this.val = val; }
9+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
10+
* }
11+
*/
112

13+
class Solution {
14+
// Two pointer approach
15+
public ListNode oddEvenList(ListNode head) {
16+
if(head == null || head.next == null)
17+
return head;
18+
19+
ListNode odd = head;
20+
ListNode h_odd = head;
21+
ListNode even = head.next;
22+
ListNode h_even = head.next;
23+
24+
while(even != null) {
25+
if(even.next == null) {
26+
odd.next = h_even;
27+
return h_odd;
28+
}
29+
odd.next = even.next;
30+
odd = odd.next;
31+
even.next = odd.next;
32+
even= even.next;
33+
}
34+
odd.next = h_even;
35+
36+
return h_odd;
37+
}
38+
}

0 commit comments

Comments
 (0)