一、原题要求:
给定一个链表,判断链表中是否有环。
解题思路: 判断是否带环:用快慢指针。快指针每走两步,慢指针走一步,如果两者在某个点相遇,则链表带环。
二、代码实现:
/**
* 原题要求:
* 给定一个链表,判断链表中是否有环。
* 思路:
* 判断是否带环:用快慢指针。快指针每走两步,慢指针走一步,如果两者在某个点相遇,则链表带环。
*
* @ClassName: Main
* @Description: TODO
* @Author: Mr.Ye
* @Data: 2018-11-24 18:33
* @Version: 1.0
**/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public boolean hasCycle(ListNode head) {
if (head == null || head.next == null){
return false;
}
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
return true;
}
}
return false;
}
}
public class Main {
public static void main(String[] args) {
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(4);
ListNode n3 = new ListNode(3);
ListNode n4 = new ListNode(2);
ListNode n5 = new ListNode(5);
ListNode n6 = new ListNode(2);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
n5.next = n6;
n6.next = n4;
Solution solution = new Solution();
System.out.println(solution.hasCycle(n1));
}
public static void print(ListNode head) {
for (ListNode temp = head; temp != null; temp = temp.next) {
System.out.print(temp.val + "->");
}
System.out.println("null");
}
}
运行结果: