Skip to content

Commit add1218

Browse files
Merge pull request #40 from IMC666/master
082 作业提交 五道链表题
2 parents b7bd688 + bede3ff commit add1218

File tree

7 files changed

+344
-0
lines changed

7 files changed

+344
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
static HeadList.ListNode detectCycle(HeadList.ListNode head){
2+
Map<HeadList.ListNode,Integer> map = new HashMap<>();
3+
HeadList.ListNode tmp = head;
4+
int pos = 0;
5+
while (tmp != null){ //JC: tmp.next == null
6+
if (map.get(tmp) == null){ //JC: map.get() return something
7+
map.put(tmp,pos);
8+
tmp = tmp.next;
9+
pos++;
10+
}
11+
else break;
12+
}
13+
return tmp;
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
static HeadList.ListNode mergeTwoLists(HeadList.ListNode l1, HeadList.ListNode l2){
2+
HeadList list = new HeadList();
3+
HeadList.ListNode tmp1 = l1.next; //because of head node.
4+
HeadList.ListNode tmp2 = l2.next;
5+
while (true){
6+
if (tmp1 == null){
7+
for( ; tmp2 != null; tmp2 = tmp2.next){
8+
list.insert(tmp2.val);
9+
}
10+
break;
11+
}
12+
else if (tmp2 == null){
13+
for( ; tmp1 != null; tmp1 = tmp1.next){
14+
list.insert(tmp1.val);
15+
}
16+
break;
17+
}
18+
else if (tmp1.val > tmp2.val){
19+
list.insert(tmp2.val);
20+
tmp2 = tmp2.next;
21+
}
22+
else {
23+
list.insert(tmp1.val);
24+
tmp1 = tmp1.next;
25+
}
26+
}
27+
return list.getHead();
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
static HeadList.ListNode swapPairs(HeadList.ListNode head){
2+
HeadList.ListNode pre, tmp, tail;
3+
pre = head;
4+
while (true){
5+
if (pre.next == null) break;
6+
if (pre.next.next == null) break;
7+
tmp = pre.next;
8+
tail = pre.next.next.next;
9+
pre.next = pre.next.next;
10+
pre.next.next = tmp;
11+
tmp.next = tail;
12+
pre = pre.next.next;
13+
}
14+
return head;
15+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
static HeadList.ListNode reverseKGroup(HeadList.ListNode head, int k){
2+
if(k == 1 || k == 0) return head;
3+
HeadList.ListNode detect, pre, segPre, segTmp, tail, tailNext;
4+
if (head.next == null) return head;
5+
segPre = head;
6+
detect = pre = segTmp = head.next;
7+
if (head.next.next == null) return head;
8+
tail = pre.next;
9+
tailNext = tail.next;
10+
while (true){
11+
int i = 0;
12+
for ( ; i < k; i++){
13+
if (detect == null){
14+
segPre.next = tailNext;
15+
return head;
16+
}
17+
else detect = detect.next;
18+
}
19+
for (int j = 0; j < k - 1; j++){ //K-1:N elements need N reverses.
20+
tail.next = pre;
21+
pre = tail;
22+
23+
tail = tailNext;
24+
if (tailNext != null) tailNext = tailNext.next;
25+
26+
}
27+
segPre.next = pre;
28+
pre = tail;
29+
tail = tailNext;
30+
if (tailNext != null) tailNext = tailNext.next;
31+
segPre = segTmp;
32+
segTmp = pre;
33+
}
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
static HeadList.ListNode deleteDuplicates(HeadList.ListNode head) {
2+
Map<Integer,HeadList.ListNode> map = new HashMap<>();
3+
HeadList.ListNode tmp = head;
4+
while (tmp != null){ //JC: tmp.next == null
5+
if(map.get(tmp.val) == null){ //JC: map.get() return something
6+
map.put(tmp.val,tmp);
7+
}
8+
else HeadList.delete(head,tmp);
9+
tmp = tmp.next;
10+
}
11+
return head;
12+
}

Week_01/id_82/链表/Main.java

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
import java.util.Scanner;
5+
6+
public class Main {
7+
8+
public static void main(String[] args) {
9+
// Output.printf(Solution.deleteDuplicates(Input.getHeadList().getHead()));
10+
// Output.printf(Solution.mergeTwoLists(Input.getHeadList().getHead(),Input.getHeadList().getHead()));
11+
// Output.printf(Solution.swapPairs(Input.getHeadList().getHead()));
12+
// HeadList list = Input.getHeadList();
13+
// Output.printf(HeadList.searchNode(Solution.detectCycle(HeadList.createCycle(Input.getPositionOrK(),list)),list));
14+
// Output.printf(Solution.reverseKGroup(Input.getHeadList().getHead(),Input.getPositionOrK()));
15+
16+
17+
}
18+
19+
}
20+
21+
/* Here is Interface class
22+
Name: ListNode
23+
*/
24+
class Solution{
25+
static HeadList.ListNode deleteDuplicates(HeadList.ListNode head) {
26+
Map<Integer,HeadList.ListNode> map = new HashMap<>();
27+
HeadList.ListNode tmp = head;
28+
while (tmp != null){ //JC: tmp.next == null
29+
if(map.get(tmp.val) == null){ //JC: map.get() return something
30+
map.put(tmp.val,tmp);
31+
}
32+
else HeadList.delete(head,tmp);
33+
tmp = tmp.next;
34+
}
35+
return head;
36+
}
37+
static HeadList.ListNode mergeTwoLists(HeadList.ListNode l1, HeadList.ListNode l2){
38+
HeadList list = new HeadList();
39+
HeadList.ListNode tmp1 = l1.next; //because of head node.
40+
HeadList.ListNode tmp2 = l2.next;
41+
while (true){
42+
if (tmp1 == null){
43+
for( ; tmp2 != null; tmp2 = tmp2.next){
44+
list.insert(tmp2.val);
45+
}
46+
break;
47+
}
48+
else if (tmp2 == null){
49+
for( ; tmp1 != null; tmp1 = tmp1.next){
50+
list.insert(tmp1.val);
51+
}
52+
break;
53+
}
54+
else if (tmp1.val > tmp2.val){
55+
list.insert(tmp2.val);
56+
tmp2 = tmp2.next;
57+
}
58+
else {
59+
list.insert(tmp1.val);
60+
tmp1 = tmp1.next;
61+
}
62+
}
63+
return list.getHead();
64+
}
65+
static HeadList.ListNode swapPairs(HeadList.ListNode head){
66+
HeadList.ListNode pre, tmp, tail;
67+
pre = head;
68+
while (true){
69+
if (pre.next == null) break;
70+
if (pre.next.next == null) break;
71+
tmp = pre.next;
72+
tail = pre.next.next.next;
73+
pre.next = pre.next.next;
74+
pre.next.next = tmp;
75+
tmp.next = tail;
76+
pre = pre.next.next;
77+
}
78+
return head;
79+
}
80+
static HeadList.ListNode detectCycle(HeadList.ListNode head){
81+
Map<HeadList.ListNode,Integer> map = new HashMap<>();
82+
HeadList.ListNode tmp = head;
83+
int pos = 0;
84+
while (tmp != null){ //JC: tmp.next == null
85+
if (map.get(tmp) == null){ //JC: map.get() return something
86+
map.put(tmp,pos);
87+
tmp = tmp.next;
88+
pos++;
89+
}
90+
else break;
91+
}
92+
return tmp;
93+
}
94+
static HeadList.ListNode reverseKGroup(HeadList.ListNode head, int k){
95+
if(k == 1 || k == 0) return head;
96+
HeadList.ListNode detect, pre, segPre, segTmp, tail, tailNext;
97+
if (head.next == null) return head;
98+
segPre = head;
99+
detect = pre = segTmp = head.next;
100+
if (head.next.next == null) return head;
101+
tail = pre.next;
102+
tailNext = tail.next;
103+
while (true){
104+
int i = 0;
105+
for ( ; i < k; i++){
106+
if (detect == null){
107+
segPre.next = tailNext;
108+
return head;
109+
}
110+
else detect = detect.next;
111+
}
112+
for (int j = 0; j < k - 1; j++){ //K-1:N elements need N reverses.
113+
tail.next = pre;
114+
pre = tail;
115+
116+
tail = tailNext;
117+
if (tailNext != null) tailNext = tailNext.next;
118+
119+
}
120+
segPre.next = pre;
121+
pre = tail;
122+
tail = tailNext;
123+
if (tailNext != null) tailNext = tailNext.next;
124+
segPre = segTmp;
125+
segTmp = pre;
126+
}
127+
}
128+
}
129+
130+
131+
/* Here is Supporting class
132+
Name: HeadList
133+
*/
134+
class HeadList{
135+
static class ListNode{
136+
int val;
137+
ListNode next;
138+
ListNode(int x){
139+
val = x;
140+
}
141+
}
142+
private ListNode head;
143+
144+
HeadList() {
145+
head = new ListNode(Integer.MIN_VALUE);
146+
}
147+
148+
ListNode getHead(){
149+
return this.head;
150+
}
151+
static boolean delete(ListNode head,ListNode node){
152+
if (head == null || node == null) return false;
153+
ListNode tmp = head;
154+
while (tmp.next != node && tmp.next != null){ //JC: tmp.next == node || tmp.next == null
155+
tmp = tmp.next;
156+
}
157+
if (tmp.next == node){
158+
tmp.next = node.next;
159+
return true;
160+
}
161+
else return false;
162+
}
163+
void insert(int x){
164+
ListNode tmp = new ListNode(x);
165+
ListNode tail = head;
166+
while (tail.next !=null){
167+
tail = tail.next;
168+
}
169+
tail.next = tmp;
170+
}
171+
static ListNode createCycle(int pos, HeadList list){
172+
ListNode tail = list.head;
173+
while (tail.next !=null){ //get tail node.
174+
tail = tail.next;
175+
}
176+
ListNode pre = list.head;
177+
while (true){
178+
for (int i = 0; i < pos+1; i++){ //because of the head node.
179+
pre = pre.next;
180+
}
181+
break;
182+
}
183+
tail.next = pre;
184+
return list.head;
185+
}
186+
static int searchNode(ListNode node, HeadList list){
187+
ListNode tmp = list.head;
188+
int index = 0;
189+
while (node != tmp){ //JC: node == tmp
190+
if (tmp != null){
191+
tmp = tmp.next;
192+
index++;
193+
}
194+
else return -1;
195+
}
196+
return index - 1;
197+
}
198+
199+
200+
}
201+
202+
203+
/* Here is Interface class
204+
Name: Output Input
205+
*/
206+
class Output{
207+
static void printf(HeadList.ListNode head){
208+
HeadList.ListNode tmp = head;
209+
while (tmp.next != null){
210+
System.out.print(tmp.next.val);
211+
tmp = tmp.next;
212+
213+
}
214+
}
215+
static void printf(int val){
216+
System.out.print(val);
217+
}
218+
}
219+
class Input{
220+
static HeadList getHeadList(){
221+
Scanner sc = new Scanner(System.in);
222+
int length = sc.nextInt();
223+
int[] array = new int[length];
224+
for (int i = 0; i < array.length; i++){
225+
array[i] = sc.nextInt();
226+
}
227+
HeadList list = new HeadList();
228+
for (int i : array){
229+
list.insert(i);
230+
}
231+
return list;
232+
}
233+
static int getPositionOrK(){
234+
Scanner sc = new Scanner(System.in);
235+
return sc.nextInt();
236+
}
237+
}
238+

Week_01/id_82/链表/ReadMe.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
以上是关于链表的五道问题的解。
2+
Main.java是以上方法实现的全部调用类。
3+
但是没有测试模块,欢迎指正错误。

0 commit comments

Comments
 (0)