[Java]LeetCode141. 環形鏈表 | Linked List Cycle

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-gftxdwzt-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a linked list, determine if it has a cycle in it.git

Follow up:
Can you solve it without using extra space?github


給定一個鏈表,判斷鏈表中是否有環。微信

進階:
你可否不使用額外空間解決此題?spa


 0ms 
 1 /**
 2  * Definition for singly-linked list.
 3  * class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public boolean hasCycle(ListNode head) {
14         if(head == null) return false;
15         ListNode fast = head;
16         ListNode slow = head;
17         do{
18             if(fast.next == null || fast.next.next == null) return false;
19             fast = fast.next.next;
20             slow = slow.next;
21         }while(fast != slow);
22         return true;
23     }
24 }

0mscode

 1 /**
 2  * Definition for singly-linked list.
 3  * class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public boolean hasCycle(ListNode head) {
14     if(head==null) return false;
15     ListNode walker = head;
16     ListNode runner = head;
17     while(runner.next!=null && runner.next.next!=null) {
18         walker = walker.next;
19         runner = runner.next.next;
20         if(walker==runner) return true;
21     }
22     return false;
23 }
24 }

1mshtm

 1 /**
 2  * Definition for singly-linked list.
 3  * class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13 public static boolean hasCycle(ListNode head) {
14         if (head == null || head.next == null) {
15             return false;
16         }
17         ListNode slow = head;
18         ListNode fast = head.next;
19         int f = 1;//上面兩行賦值已前進了一次
20         while (slow != fast) {
21             if (fast == null || fast.next == null) {
22                 return false;
23             }
24             slow = slow.next;
25             fast = fast.next.next;
26             f++;
27         }
28         System.out.println("LinkedList.size:"+f);
29         return true;
30     }
31 }

4msblog

 1 public class Solution {
 2     public boolean hasCycle(ListNode head) {
 3         HashSet<ListNode> visited = new HashSet<>();
 4         
 5         ListNode curr = head;
 6         while (curr != null) {
 7             if (visited.contains(curr))
 8                 return true;
 9             visited.add(curr);
10             curr = curr.next;
11         }
12         
13         return false;
14     }
15 }

5msip

 1 /**
 2  * Definition for singly-linked list.
 3  * class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public boolean hasCycle(ListNode head) {
14         Set<ListNode> set = new HashSet<>();
15         while(head != null){
16             if(set.contains(head)){
17                 return true;
18             } else{
19                 set.add(head);
20             }
21             head = head.next;
22         }
23         return false;
24     }
25 }
相關文章
相關標籤/搜索