Skip to content

Commit 4724763

Browse files
author
chenweijie
committed
其它解法
1 parent 0d44125 commit 4724763

File tree

3 files changed

+238
-0
lines changed

3 files changed

+238
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.chen.algorithm.study.test25;
2+
3+
/**
4+
* @author : chen weijie
5+
* @Date: 2019-09-02 23:06
6+
* 参考 Solution3
7+
*/
8+
public class ListNode {
9+
10+
int val;
11+
12+
ListNode next;
13+
14+
ListNode() {
15+
}
16+
17+
ListNode(int val) {
18+
this.val = val;
19+
}
20+
21+
ListNode(int val, ListNode next) {
22+
this.val = val;
23+
this.next = next;
24+
}
25+
26+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.chen.algorithm.study.test25;
2+
3+
/**
4+
* @Auther: zhunn
5+
* @Date: 2020/10/24 17:23
6+
* @Description: K个一组翻转链表
7+
*/
8+
public class Solution5 {
9+
10+
/**
11+
* 官网解法
12+
* @param head
13+
* @param k
14+
* @return
15+
*/
16+
public ListNode reverseKGroup(ListNode head, int k) {
17+
ListNode hair = new ListNode(0);
18+
hair.next = head;
19+
ListNode pre = hair;
20+
21+
while (head != null) {
22+
ListNode tail = pre;
23+
// 查看剩余部分长度是否大于等于 k
24+
for (int i = 0; i < k; ++i) {
25+
tail = tail.next;
26+
if (tail == null) {
27+
return hair.next;
28+
}
29+
}
30+
ListNode nextTemp = tail.next;
31+
ListNode[] reverse = myReverse(head, tail);
32+
head = reverse[0];
33+
tail = reverse[1];
34+
// 把子链表重新接回原链表
35+
pre.next = head;
36+
tail.next = nextTemp;
37+
pre = tail;
38+
head = tail.next;
39+
}
40+
41+
return hair.next;
42+
}
43+
44+
private ListNode[] myReverse(ListNode head, ListNode tail) {
45+
ListNode prev = tail.next;
46+
ListNode p = head;
47+
while (prev != tail) {
48+
ListNode nex = p.next;
49+
p.next = prev;
50+
prev = p;
51+
p = nex;
52+
}
53+
return new ListNode[]{tail, head};
54+
}
55+
56+
57+
public ListNode reverseKGroup1(ListNode head, int k) {
58+
ListNode dummy = new ListNode(0);
59+
dummy.next = head;
60+
61+
ListNode pre = dummy;
62+
ListNode end = dummy;
63+
64+
while (end.next != null) {
65+
for (int i = 0; i < k && end != null; i++){end = end.next;}
66+
if (end == null) {break;}
67+
ListNode start = pre.next;
68+
ListNode next = end.next;
69+
end.next = null;
70+
pre.next = reverse(start);
71+
start.next = next;
72+
pre = start;
73+
74+
end = pre;
75+
}
76+
return dummy.next;
77+
}
78+
79+
private ListNode reverse(ListNode head) {
80+
ListNode pre = null;
81+
ListNode curr = head;
82+
while (curr != null) {
83+
ListNode next = curr.next;
84+
curr.next = pre;
85+
pre = curr;
86+
curr = next;
87+
}
88+
return pre;
89+
}
90+
91+
92+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.chen.algorithm.study.test92;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @Auther: zhunn
7+
* @Date: 2020/10/24 17:23
8+
* @Description: 反转链表二:1-双指针,2-删除结点递推
9+
*/
10+
public class Solution3 {
11+
12+
class ListNode {
13+
int val;
14+
ListNode next;
15+
16+
ListNode(int x) {
17+
val = x;
18+
}
19+
}
20+
21+
// 翻转n个节点,返回新链表的头部
22+
private ListNode reverseN(ListNode head, int n) {
23+
ListNode prev = null;
24+
ListNode curr = head;
25+
for (int i = 0; i < n; i++) {
26+
ListNode oldNext = curr.next;
27+
curr.next = prev;
28+
prev = curr;
29+
curr = oldNext;
30+
}
31+
return prev;
32+
}
33+
34+
/**
35+
* 1-双指针
36+
* @param head
37+
* @param m
38+
* @param n
39+
* @return
40+
*/
41+
public ListNode reverseBetween1(ListNode head, int m, int n) {
42+
ListNode dummy = new ListNode(42);
43+
dummy.next = head;
44+
ListNode ptr1 = dummy;
45+
ListNode ptr2 = dummy;
46+
// 找到左右两段的端点
47+
for (int i = 0; i < m - 1; i++) {
48+
ptr2 = ptr2.next;
49+
}
50+
for (int i = 0; i < n + 1; i++) {
51+
ptr1 = ptr1.next;
52+
}
53+
// 找到中段的尾端点
54+
ListNode t = ptr2.next;
55+
// 翻转中段,得到中段的头端点
56+
ListNode h = reverseN(t, n - m + 1);
57+
// 中端的头端点和左段端点相连
58+
ptr2.next = h;
59+
// 中段的尾端点和右段端点相连
60+
t.next = ptr1;
61+
return dummy.next;
62+
}
63+
64+
/**
65+
* 2-删除结点递推
66+
* @param head
67+
* @param m
68+
* @param n
69+
* @return
70+
*/
71+
public ListNode reverseBetween2(ListNode head, int m, int n){
72+
if(head == null || head.next == null){
73+
return head;
74+
}
75+
76+
ListNode dummy = new ListNode(-1);
77+
dummy.next = head;
78+
ListNode pre = dummy;
79+
for(int i =0;i<m-1;i++){
80+
pre = pre.next;
81+
head = head.next;
82+
}
83+
84+
for (int i = m; i <n ; i++) {
85+
ListNode nextTemp = head.next;
86+
head.next =nextTemp.next;
87+
nextTemp.next = pre.next;
88+
pre.next = nextTemp;
89+
}
90+
return dummy.next;
91+
}
92+
93+
@Test
94+
public void test() {
95+
ListNode l1_1 = new ListNode(7);
96+
ListNode l1_2 = new ListNode(9);
97+
ListNode l1_3 = new ListNode(2);
98+
ListNode l1_4 = new ListNode(10);
99+
ListNode l1_5 = new ListNode(1);
100+
ListNode l1_6 = new ListNode(8);
101+
ListNode l1_7 = new ListNode(6);
102+
103+
l1_1.next = l1_2;
104+
l1_2.next = l1_3;
105+
l1_3.next = l1_4;
106+
l1_4.next = l1_5;
107+
l1_5.next = l1_6;
108+
l1_6.next = l1_7;
109+
110+
ListNode result = reverseBetween1(l1_1,3,6);
111+
112+
System.out.println(result.val);
113+
System.out.println(result.next.val);
114+
System.out.println(result.next.next.val);
115+
System.out.println(result.next.next.next.val);
116+
System.out.println(result.next.next.next.next.val);
117+
System.out.println(result.next.next.next.next.next.val);
118+
System.out.println(result.next.next.next.next.next.next.val);
119+
}
120+
}

0 commit comments

Comments
 (0)