-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortOddEvenList.java
More file actions
63 lines (60 loc) · 1.71 KB
/
SortOddEvenList.java
File metadata and controls
63 lines (60 loc) · 1.71 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
59
60
61
62
63
package codeTop;
/**
* 给定一个奇数位升序,偶数位降序的链表,将其重新排序。
*
* 输入: 1->8->3->6->5->4->7->2->NULL
* 输出: 1->2->3->4->5->6->7->8->NULL
*/
public class SortOddEvenList {
class ListNode{
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public ListNode sortOddEvenList(ListNode root){
if(root == null || root.next == null){
return root;
}
ListNode oddHead = root,evenHead = root.next;
ListNode cur1 = oddHead,cur2 = evenHead;
//将链表分成两部分
while(cur2 != null && cur2.next != null){
cur1.next = cur2.next;
cur2.next = cur2.next.next;
cur1 = cur1.next;
cur2 = cur2.next;
}
//将偶数位置的链表翻转
ListNode pre = null;
ListNode cur = evenHead;
ListNode next = null;
while(cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
//合并两个链表
ListNode dumpy = new ListNode(0);
ListNode tmp = dumpy;
while(pre != null && oddHead != null){
if(pre.val <= oddHead.val){
tmp.next = pre;
pre = pre.next;
}else{
tmp.next = oddHead;
oddHead = oddHead.next;
}
tmp = tmp.next;
}
if(pre != null){
tmp.next = pre;
}
if(oddHead != null){
tmp.next = oddHead;
}
return dumpy.next;
}
}