Maybe a weird solution,but a plausible one


  • -2
    A
    public class Solution {
            public boolean hasCycle(ListNode head){
        		ListNode l1 = head;
        		if(head == null)
        		    return false;
        		int max = Integer.MAX_VALUE;
        		head.val = max;
        		
        		while(l1.next != null)
        		{
        		    l1 = l1.next;
        		    if(l1.val == max)
        				return true;
        			l1.val = max;
        		}
        		return false;
        	}
        }

  • 0

    what if an original node has the max integer value? maybe could use null.


  • 0
    A

    yes,this is risky but kind of feasible only when you are occasionally stuck in a emergency situation


  • 0
    Z

    But you completely changed the LinkedList. The linked list is useless after running the check.


  • 0

    That's true.Seems the only way is to use two pointers


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.