Skip to content

Commit f982332

Browse files
author
Your Name
committed
反转列表
1 parent 0d17728 commit f982332

File tree

4 files changed

+229
-2
lines changed

4 files changed

+229
-2
lines changed

progress.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
easy :
2+
列表操作 21 83
3+
4+
medium:
5+
6+
hard:
7+
8+
9+
10+
可以分为基础的数据结构,比如数组、链表、栈、队列、二叉树、堆的使用,这几种常见的数据结构的基础操作一定要很熟悉,
11+
比如链表逆置、删除、获取第 K 个元素、判断是否有环等,二叉树翻转、深度遍历、层级遍历、求树深度、公共父节点等。

src/com/blankj/easy/_0021/Solution.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.blankj.easy._021;
22

33
import com.blankj.structure.ListNode;
4+
import com.blankj.structure.ListNodeMe;
5+
6+
import static java.lang.System.out;
47

58
/**
69
* <pre>
@@ -28,12 +31,38 @@ public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
2831
return head.next;
2932
}
3033

31-
public static void main(String[] args) {
34+
public static void main2(String[] args) {
3235
Solution solution = new Solution();
3336
ListNode listNode0 = ListNode.createTestData("[1,3,5,7,9]");
3437
ListNode listNode1 = ListNode.createTestData("[2,4,6,8,10]");
3538
ListNode.print(listNode0);
3639
ListNode.print(listNode1);
3740
ListNode.print(solution.mergeTwoLists(listNode0, listNode1));
3841
}
42+
43+
public static void main(String[] args) {
44+
Solution solution = new Solution();
45+
ListNodeMe listNode0 = ListNodeMe.createTestData("[1,3,5,7,9]");
46+
ListNodeMe listNode1 = ListNodeMe.createTestData("[1,2,4,6,8,10]");
47+
ListNodeMe.print(listNode0);
48+
ListNodeMe.print(listNode1);
49+
ListNodeMe.print(solution.mergeTwoLists(listNode0, listNode1));
50+
}
51+
52+
private ListNodeMe mergeTwoLists(ListNodeMe listNode0, ListNodeMe listNode1) {
53+
ListNodeMe header = new ListNodeMe(-1);
54+
ListNodeMe temp = header;
55+
while (listNode0 != null && listNode1 != null) {
56+
if (listNode0.data < listNode1.data) {
57+
temp.next = listNode0;
58+
listNode0 = listNode0.next;
59+
} else {
60+
temp.next = listNode1;
61+
listNode1 = listNode1.next;
62+
}
63+
temp = temp.next;
64+
}
65+
temp.next = listNode0 == null ? listNode1 : listNode0;
66+
return header.next;
67+
}
3968
}

src/com/blankj/easy/_0083/Solution.java

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package com.blankj.easy._083;
22

33
import com.blankj.structure.ListNode;
4+
import com.blankj.structure.ListNodeMe;
5+
6+
import java.util.Collections;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
import static java.lang.System.in;
11+
import static java.lang.System.out;
412

513
/**
614
* <pre>
@@ -24,9 +32,119 @@ public ListNode deleteDuplicates(ListNode head) {
2432
return head;
2533
}
2634

27-
public static void main(String[] args) {
35+
public static void main2(String[] args) {
2836
Solution solution = new Solution();
2937
ListNode.print(solution.deleteDuplicates(ListNode.createTestData("[1,1,2]")));
3038
ListNode.print(solution.deleteDuplicates(ListNode.createTestData("[1,1,2,3,3]")));
3139
}
40+
41+
public static void main_me(String[] args) {
42+
Solution solution = new Solution();
43+
// 升序的链表排重很好做了
44+
ListNodeMe listNode1 = ListNodeMe.createTestData("[1,2,2,4,4,6,8,10]");
45+
ListNodeMe.print(listNode1);
46+
solution.deleteDuplicates2(listNode1);
47+
}
48+
49+
public static void main3(String[] args) {
50+
Solution solution = new Solution();
51+
// ListNodeMe listNode1 = ListNodeMe.createTestData("[1,2,2,4,4,6,8,10]");
52+
ListNodeMe listNode1 = ListNodeMe.createCirlceList("[1,2,2,4,4,6,8,10]");
53+
boolean looperList = solution.isLooperList(listNode1);
54+
out.println("is cirlce list ? " + looperList);
55+
}
56+
57+
public static void main(String[] args) {
58+
Solution solution = new Solution();
59+
ListNodeMe listNode1 = ListNodeMe.createTestData("[1,2,3,4,5,6,8,10]");
60+
// ListNodeMe.print(solution.reverseList2(listNode1));
61+
listNode1 = solution.deleteByIndex(listNode1, 3);
62+
63+
ListNodeMe.print(listNode1);
64+
}
65+
66+
private void deleteDuplicates2(ListNodeMe listNode1) {
67+
ListNodeMe temp = listNode1;
68+
while (temp != null && temp.next != null) {
69+
if (temp.data == temp.next.data) {
70+
temp.next = temp.next.next;
71+
} else {
72+
temp = temp.next;
73+
}
74+
}
75+
ListNodeMe.print(listNode1);
76+
}
77+
78+
/**
79+
* 判断是否有环
80+
* @param temp
81+
* @return
82+
*/
83+
private boolean isLooperList(ListNodeMe temp) {
84+
Object o = new Object();
85+
Map<ListNodeMe, Object> map = new HashMap<>();
86+
while (temp != null) {
87+
Object put = map.put(temp, o);
88+
if (put != null) {
89+
return true;
90+
}
91+
temp = temp.next;
92+
}
93+
return false;
94+
}
95+
96+
public static ListNodeMe reverseList2(ListNodeMe current) {
97+
ListNodeMe pre = null;
98+
ListNodeMe next = null;
99+
while (current != null) {
100+
// 第一步: 保存下一个节点,因为需要打断
101+
next = current.next;
102+
// 第二步:打断
103+
current.next = pre;
104+
105+
//后面两步为了下一次循环做准备
106+
pre = current;
107+
current = next;
108+
}
109+
return pre;
110+
}
111+
112+
/**
113+
* 删除第index个元素, 从0开始 "[1,2,3,4,5,6,8,10]" 2
114+
* @param current
115+
* @param index
116+
*/
117+
public ListNodeMe deleteByIndex(ListNodeMe current, int index) { // 2
118+
ListNodeMe orign = current;
119+
if (index == 0) {
120+
return orign.next;
121+
}
122+
ListNodeMe pre = null;
123+
while (index > 0) {
124+
pre = orign;
125+
orign = orign.next;
126+
index--;
127+
}
128+
pre.next = orign.next;
129+
orign.next = null;
130+
return current;
131+
}
132+
133+
/**
134+
* 反转列表
135+
* @param current
136+
* @return
137+
*/
138+
public static ListNodeMe reverseList(ListNodeMe current) {
139+
ListNodeMe pre = null;
140+
ListNodeMe next = null;
141+
while (current != null) {
142+
next = current.next;
143+
current.next = pre;
144+
145+
pre = current;
146+
current = next;
147+
}
148+
return pre;
149+
}
32150
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.blankj.structure;
2+
3+
import static java.lang.System.out;
4+
5+
public class ListNodeMe {
6+
public int data;
7+
public ListNodeMe next;
8+
9+
public ListNodeMe(int data) {
10+
this.data = data;
11+
}
12+
13+
/**
14+
* 创建测试数据
15+
*
16+
* @param data [XX,XX,XX]
17+
* @return {@link ListNode}
18+
*/
19+
public static ListNodeMe createTestData(String data) {
20+
if ("[]".equals(data)) {
21+
return null;
22+
}
23+
String substring = data.substring(1, data.length()-1);
24+
out.println(substring);
25+
String[] arr = substring.split(",");
26+
ListNodeMe[] nodeArr = new ListNodeMe[arr.length];
27+
nodeArr[0] = new ListNodeMe(Integer.parseInt(arr[0]));
28+
for (int i = 1; i < arr.length; i++) {
29+
nodeArr[i] = new ListNodeMe(Integer.parseInt(arr[i]));
30+
nodeArr[i-1].next = nodeArr[i];
31+
}
32+
return nodeArr[0];
33+
}
34+
35+
/**
36+
* 创建带有环的链表
37+
* @param data
38+
* @return
39+
*/
40+
public static ListNodeMe createCirlceList(String data) {
41+
if ("[]".equals(data)) {
42+
return null;
43+
}
44+
String substring = data.substring(1, data.length()-1);
45+
out.println(substring);
46+
String[] arr = substring.split(",");
47+
ListNodeMe[] nodeArr = new ListNodeMe[arr.length];
48+
nodeArr[0] = new ListNodeMe(Integer.parseInt(arr[0]));
49+
for (int i = 1; i < arr.length; i++) {
50+
nodeArr[i] = new ListNodeMe(Integer.parseInt(arr[i]));
51+
nodeArr[i-1].next = nodeArr[i];
52+
}
53+
nodeArr[arr.length - 1].next = nodeArr[0];
54+
return nodeArr[0];
55+
}
56+
57+
public static void print(ListNodeMe nodeMe) {
58+
while (nodeMe != null) {
59+
out.print(nodeMe.data + " ");
60+
nodeMe = nodeMe.next;
61+
}
62+
out.println();
63+
}
64+
65+
public static void main(String[] args) {
66+
ListNodeMe nodeMe = ListNodeMe.createTestData("[1,3,5,7,9]");
67+
print(nodeMe);
68+
}
69+
}

0 commit comments

Comments
 (0)