Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd62acf0

Browse files
committed
hascycle
1 parent5c5e0ac commitd62acf0

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

‎list/HasCycle.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
packageAlgorithms.list;
2+
3+
importAlgorithms.algorithm.others.ListNode;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) {
11+
* val = x;
12+
* next = null;
13+
* }
14+
* }
15+
*/
16+
publicclassHasCycle {
17+
publicbooleanhasCycle(ListNodehead) {
18+
if (head ==null) {
19+
returnfalse;
20+
}
21+
22+
ListNodeslow =head;
23+
ListNodefast =head;
24+
25+
while (fast !=null &&fast.next !=null) {
26+
slow =slow.next;
27+
fast =fast.next.next;
28+
if (slow ==fast) {
29+
returntrue;
30+
}
31+
}
32+
33+
returnfalse;
34+
}
35+
}

‎list/RotateList.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
* }
1515
*/
1616
publicclassRotateList {
17-
publicListNoderotateRight(ListNodehead,intn) {
17+
publicstaticvoidmain(String[]strs) {
18+
ListNodenode1 =newListNode(1);
19+
ListNoderet =rotateRight(node1,0);
20+
System.out.println(ret.toString());
21+
}
22+
23+
publicstaticListNoderotateRight(ListNodehead,intn) {
1824
if (head ==null) {
1925
returnnull;
2026
}
@@ -51,7 +57,7 @@ public ListNode rotateRight(ListNode head, int n) {
5157
}
5258

5359
// get the list lenght.
54-
publicintgetlen(ListNodehead) {
60+
publicstaticintgetlen(ListNodehead) {
5561
intlen =0;
5662
while (head !=null) {
5763
len++;

‎list/RotateRight.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
packageAlgorithms.list;
2+
3+
importAlgorithms.algorithm.others.ListNode;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* public class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) {
11+
* val = x;
12+
* next = null;
13+
* }
14+
* }
15+
*/
16+
publicclassRotateRight {
17+
// Solution 1:
18+
publicListNoderotateRight1(ListNodehead,intn) {
19+
if (head ==null) {
20+
returnhead;
21+
}
22+
23+
intlen =getLen(head);
24+
25+
// 不需要重复地rotate.
26+
n =n %len;
27+
28+
if (n ==0) {
29+
returnhead;
30+
}
31+
32+
ListNodeend =head;
33+
while (n >0) {
34+
end =end.next;
35+
n--;
36+
}
37+
38+
ListNodepre =head;
39+
while (end.next !=null) {
40+
pre =pre.next;
41+
end =end.next;
42+
}
43+
44+
ListNodeheadNew =pre.next;
45+
end.next =head;
46+
pre.next =null;
47+
48+
returnheadNew;
49+
}
50+
51+
publicintgetLen(ListNodehead) {
52+
intlen =0;
53+
while (head !=null) {
54+
len++;
55+
head =head.next;
56+
}
57+
returnlen;
58+
}
59+
60+
// Solution 2: 使用dummynode.
61+
publicListNoderotateRight(ListNodehead,intn) {
62+
if (head ==null) {
63+
returnhead;
64+
}
65+
66+
intlen =getLen(head);
67+
68+
// 不需要重复地rotate.
69+
n =n %len;
70+
71+
ListNodedummy =newListNode(0);
72+
dummy.next =head;
73+
74+
ListNodeend =dummy;
75+
while (n >0) {
76+
end =end.next;
77+
n--;
78+
}
79+
80+
ListNodepre =dummy;
81+
while (end.next !=null) {
82+
pre =pre.next;
83+
end =end.next;
84+
}
85+
86+
end.next =dummy.next;
87+
ListNodeheadNew =pre.next;
88+
pre.next =null;
89+
90+
returnheadNew;
91+
}
92+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp