-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path141. 环形链表
More file actions
51 lines (47 loc) · 1.13 KB
/
141. 环形链表
File metadata and controls
51 lines (47 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
def my(tree):
if tree==None:
return False
else:
if tree.val==None:
return True
else:
tree.val=None
return my(tree.next)
return my(head)
# 遍历的过程中修改val值表示来过 即可
// java写法
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode a=head;
if(head==null||head.next==null){
return false;
}
ListNode b=head.next;
while (a!=null&&b!=null){
if(a==b){
return true;
}
a=a.next;
b=b.next!=null ? b.next.next:b.next;
}
return false;
}
}