-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.java
58 lines (49 loc) · 1.53 KB
/
Solution.java
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 leetcode.algo.list.leet_zh_1152;
import leetcode.algo.list.base.ListNode;
class Solution {
/*
* 328.奇偶链表
* 执行用时 : 6 ms
* 内存消耗 : 39.1 MB
* */
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode oddNode = head;
ListNode evenNode = head.next;
ListNode evenHead = evenNode;
ListNode cursor = evenNode.next;
while (oddNode.next != null && evenNode.next != null) {
oddNode.next = cursor;
oddNode = oddNode.next;
cursor = cursor.next;
evenNode.next = cursor;
evenNode = evenNode.next;
cursor = cursor.next;
}
if (cursor != null) {
oddNode.next = cursor;
oddNode = oddNode.next;
}
oddNode.next = evenHead;
evenNode.next = null;
return head;
}
public static void main(String[] args) {
ListNode listNode = new ListNode(1);
listNode.next = new ListNode(2);
listNode.next.next = new ListNode(3);
listNode.next.next.next = new ListNode(4);
listNode.next.next.next.next = new ListNode(5);
// while (listNode != null) {
// System.out.println(listNode.val);
// listNode = listNode.next;
// }
ListNode l = new Solution().oddEvenList(listNode);
while (l != null) {
System.out.println(l.val);
l = l.next;
}
}
}