Skip to content

Commit e4c5165

Browse files
committed
链表重排序
1 parent 98a7004 commit e4c5165

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

LeetCode/Doc/链表重排序.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# reorder-list(链表重排序)
2+
3+
<center>知识点:链表</center>
4+
5+
6+
## 题目描述
7+
8+
Given a singly linked list$ L: L_0→L_1→…→L{n-1}→L_n​$,
9+
reorder it to: $L_0→L_n →L_1→L_{n-1}→L_2→L_{n-2}→…​$
10+
11+
You must do this in-place without altering the nodes' values.
12+
13+
For example,
14+
Given{1,2,3,4}, reorder it to{1,4,2,3}.
15+
16+
给定一个链表$ L: L_0→L_1→…→L{n-1}→L_n$,
17+
将其重排序为: $L_0→L_n →L_1→L_{n-1}→L_2→L_{n-2}→…​$
18+
你必须在不改变节点值的情况下完成。
19+
比如:
20+
给定{1,2,3,4},重排序为:{1,4,2,3}
21+
22+
## 解题思路
23+
24+
比如:$ L: L_0→L_1→…→L{n-1}→L_n​$,首先利用快慢指针将该链表分为两个部分:
25+
26+
$L_a:L_0→L_1→…L_{\frac{n}{2}}$,
27+
28+
$L_b:L_{\frac{n}{2}+1}→L_{\frac{n}{2}+2}→…L_{n}$
29+
30+
然后将$L_b$翻转,然后合并$L_a$和$L_b$即可。
31+
32+
## 代码
33+
34+
[这里](../src/eight/Solution.java)
Binary file not shown.
Binary file not shown.

LeetCode/src/eight/Solution.java

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package eight;
2+
3+
/**
4+
* @author dmrfcoder
5+
* @date 2019/4/10
6+
*/
7+
8+
/**
9+
* Definition for singly-linked list.
10+
*/
11+
class ListNode {
12+
int val;
13+
ListNode next;
14+
15+
ListNode(int x) {
16+
val = x;
17+
next = null;
18+
}
19+
}
20+
21+
public class Solution {
22+
public void reorderList(ListNode head) {
23+
if (head != null && head.next != null) {
24+
ListNode fast, slow;
25+
fast = slow = head;
26+
//快慢指针找出链表的中间节点
27+
while (fast.next != null && fast.next.next != null) {
28+
fast = fast.next.next;
29+
slow = slow.next;
30+
}
31+
32+
//拆分链表
33+
ListNode preNode = slow.next;
34+
slow.next = null;
35+
ListNode afterNode = preNode.next;
36+
preNode.next=null;
37+
//翻转后一半链表
38+
while (afterNode != null) {
39+
ListNode tempNode = afterNode.next;
40+
afterNode.next = preNode;
41+
preNode = afterNode;
42+
afterNode = tempNode;
43+
}
44+
45+
//合并链表
46+
while (preNode != null && head != null) {
47+
ListNode tempNode1 = head.next;
48+
ListNode tempNode2 = preNode.next;
49+
head.next = preNode;
50+
preNode.next = tempNode1;
51+
preNode = tempNode2;
52+
head = tempNode1;
53+
54+
}
55+
56+
57+
}
58+
59+
}
60+
61+
62+
public static void main(String[] args) {
63+
ListNode head = new ListNode(1);
64+
ListNode node2 = new ListNode(2);
65+
ListNode node3 = new ListNode(3);
66+
ListNode node4 = new ListNode(4);
67+
ListNode node5 = new ListNode(5);
68+
69+
head.next = node2;
70+
node2.next = node3;
71+
node3.next = node4;
72+
node4.next = node5;
73+
Solution solution = new Solution();
74+
solution.reorderList(head);
75+
}
76+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,5 @@
149149
- [insertion-sort-list(使用插入排序对链表进行排序)](./LeetCode/Doc/使用插入排序对链表进行排序.md)
150150
- [binary-tree-postorder-traversal(后序遍历二叉树)](./LeetCode/Doc/后序遍历二叉树.md)
151151
- [binary-tree-preorder-traversal(先序遍历二叉树)](./LeetCode/Doc/先序遍历二叉树.md)
152+
- [reorder-list(链表重排序)](./LeetCode/Doc/链表重排序.md)
152153

0 commit comments

Comments
 (0)