-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBM2.java
More file actions
92 lines (89 loc) · 2.97 KB
/
BM2.java
File metadata and controls
92 lines (89 loc) · 2.97 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package NiukeTOP101;
public class BM2 {
/**
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
public ListNode reverseBetween (ListNode head, int m, int n) {
// 设置哑点
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode pre = dummyNode;
for(int i=0;i<m-1;i++){
pre = pre.next;
}
ListNode cur = pre.next;
ListNode cur_next;
for(int i=0;i<n-m;i++){
cur_next = cur.next;
cur.next = cur_next.next;
cur_next.next = pre.next;
pre.next = cur_next;
}
return dummyNode.next;
}
public static void main(String[] args) {
BM2 bm2 = new BM2();
ListNode nodeSta = new ListNode(0); //创建首节点
ListNode nextNode; //声明一个变量用来在移动过程中指向当前节点
nextNode=nodeSta; //指向首节点
//这句话就相当于 ListNode nextNode = nodeSta = new ListNode(0);
//创建链表
int[] x = new int[]{1,2,3,4,5};
for(int i=0;i<x.length;i++){
ListNode node = new ListNode(x[i]); //生成新的节点
nextNode.next=node; //把新节点连起来
nextNode=nextNode.next; //当前节点往后移动
} //当for循环完成之后 nextNode指向最后一个节点,
nextNode=nodeSta; //重新赋值让它指向首节点
bm2.reverseBetween(nextNode.next, 2, 4);
}
}
/**
* 双指针
*/
class BM2_1{
// 说明:方便理解,以下注释中将用 left,right 分别代替 m,n 节点
public ListNode reverseBetween(ListNode head, int m, int n){
// 设置虚拟头结点
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode pre= dummyNode;
// 1.走left-1步到left的前一个结点
for (int i = 0; i < m - 1; i++) {
pre = pre.next;
}
// 2.走right-left+1步到right结点
ListNode rightNode = pre;
for (int i = 0; i < n - m + 1; i++) {
rightNode = rightNode.next;
}
// 3.截取出一个子链表
ListNode leftNode = pre.next;
ListNode cur = rightNode.next;
// 4.切断链接
pre.next = null;
rightNode.next = null;
//5.反转局部链表
reverseLinkedList(leftNode);
//6.接回原来的链表
pre.next = rightNode;
leftNode.next = cur;
return dummyNode.next;
}
//反转局部链表
private void reverseLinkedList(ListNode head){
ListNode pre = null;
ListNode cur = head;
while(cur!=null){
//Cur_next 指向cur节点的下一个节点
ListNode cur_next = cur.next;
cur.next = pre;
pre = cur;
cur = cur_next ;
}
}
}