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

Commit 9f81c74

Browse files
增加代码
1 parent c72ba03 commit 9f81c74

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

‎java/141.环形链表.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @lc app=leetcode.cn id=141 lang=java
3+
*
4+
* [141] 环形链表
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode(int x) {
14+
* val = x;
15+
* next = null;
16+
* }
17+
* }
18+
*/
19+
public class Solution {
20+
public boolean hasCycle(ListNode head) {
21+
if(head == null) return false;
22+
ListNode slow = head;
23+
ListNode fast = head.next;
24+
25+
while(fast != null && fast.next != null) {
26+
if(slow == fast) {
27+
return true;
28+
}
29+
30+
slow = slow.next;
31+
fast = fast.next.next;
32+
}
33+
34+
return false;
35+
}
36+
}
37+
// @lc code=end
38+

‎java/142.环形链表II.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode.cn id=142 lang=java
3+
*
4+
* [142] 环形链表 II
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode(int x) {
14+
* val = x;
15+
* next = null;
16+
* }
17+
* }
18+
*/
19+
public class Solution {
20+
public ListNode detectCycle(ListNode head) {
21+
if(head == null) return null;
22+
23+
ListNode slow = head;
24+
ListNode fast = head;
25+
26+
while(fast != null && fast.next != null) {
27+
fast = fast.next.next;
28+
slow = slow.next;
29+
if(slow == fast) {
30+
fast = head;
31+
while(fast != slow) {
32+
fast = fast.next;
33+
slow = slow.next;
34+
}
35+
36+
return slow;
37+
}
38+
}
39+
40+
return null;
41+
}
42+
}
43+
// @lc code=end
44+

‎java/234.回文链表.java‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @lc app=leetcode.cn id=234 lang=java
3+
*
4+
* [234] 回文链表
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* public class ListNode {
11+
* int val;
12+
* ListNode next;
13+
* ListNode() {}
14+
* ListNode(int val) { this.val = val; }
15+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
16+
* }
17+
*/
18+
class Solution {
19+
public boolean isPalindrome(ListNode head) {
20+
if(head == null) return true;
21+
22+
ListNode slow = head;
23+
ListNode fast = head.next;
24+
25+
while(fast != null && fast.next != null) {
26+
slow = slow.next;
27+
fast = fast.next.next;
28+
}
29+
30+
ListNode tail = reverse(slow.next);
31+
slow.next = null;
32+
33+
while(head != null && tail != null) {
34+
if(head.val != tail.val) {
35+
return false;
36+
}
37+
38+
head = head.next;
39+
tail = tail.next;
40+
}
41+
42+
return true;
43+
44+
}
45+
46+
private ListNode reverse(ListNode node) {
47+
if(node == null) return null;
48+
49+
ListNode dummy = new ListNode();
50+
while(node != null) {
51+
ListNode tmp = node.next;
52+
node.next = dummy.next;
53+
dummy.next = node;
54+
node = tmp;
55+
}
56+
57+
return dummy.next;
58+
}
59+
60+
61+
}
62+
// @lc code=end
63+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /