leetcode算法题解(Java版)-7-循环链表
一、循环链表 题目描述 Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 思路 不能用多余空间,刚开始没有考虑多个指针什么,一下子想到个歪点子:循环就是重复走,那我可以标记一下每次走过的路,如果遇到标记过的路,那说明就是有回路了。 代码一 /** * 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) { if(head==null){ return false; } ListNode p=new ListNode(0); p=head; int u=-987123; wh...
