forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
76 lines (63 loc) · 1.83 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package eight;
/**
* @author dmrfcoder
* @date 2019/4/10
*/
/**
* Definition for singly-linked list.
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class Solution {
public void reorderList(ListNode head) {
if (head != null && head.next != null) {
ListNode fast, slow;
fast = slow = head;
//快慢指针找出链表的中间节点
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
//拆分链表
ListNode preNode = slow.next;
slow.next = null;
ListNode afterNode = preNode.next;
preNode.next=null;
//翻转后一半链表
while (afterNode != null) {
ListNode tempNode = afterNode.next;
afterNode.next = preNode;
preNode = afterNode;
afterNode = tempNode;
}
//合并链表
while (preNode != null && head != null) {
ListNode tempNode1 = head.next;
ListNode tempNode2 = preNode.next;
head.next = preNode;
preNode.next = tempNode1;
preNode = tempNode2;
head = tempNode1;
}
}
}
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
head.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
Solution solution = new Solution();
solution.reorderList(head);
}
}