Java 1ms reversing list


  • 1
    R
     public boolean hasCycle(ListNode head) {
        ListNode prev=null;
        ListNode curr=head;
        while(curr!=null){
            ListNode next=curr.next;
            curr.next=prev;
            prev=curr;
            curr=next;
        }
        if(prev==head&&head!=null&&head.next!=null)   //if we reach first node again, there is a cycle
            return true;
        return false;

Log in to reply
 

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