-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOddEvenLinkedList.java
More file actions
41 lines (34 loc) · 923 Bytes
/
OddEvenLinkedList.java
File metadata and controls
41 lines (34 loc) · 923 Bytes
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
package linkedlist;
import linkedlist.LinkedListTest.ListNode;
/**
* @author Shogo Akiyama
* Solved on 08/02/2019
*
* 328. Odd Even Linked List
* https://leetcode.com/problems/odd-even-linked-list/
* Difficulty: Medium
*
* Approach: Two Pointers & Iteration
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Odd Even Linked List.
* Memory Usage: 36.6 MB, less than 100.00% of Java online submissions for Odd Even Linked List.
*
* @see LinkedListTest#testOddEvenLinkedList()
*/
public class OddEvenLinkedList {
public ListNode oddEvenList(ListNode head) {
if (head == null) {
return head;
}
ListNode odd = head;
ListNode even = head.next;
ListNode firstEven = even;
while (odd.next != null && odd.next.next != null) {
odd.next = odd.next.next;
even.next = even.next.next;
odd = odd.next;
even = even.next;
}
odd.next = firstEven;
return head;
}
}