concise Java solution by reversing the list


  • 0
    Z

    public class Solution {
    public boolean hasCycle(ListNode head) {
    if(head == null||head.next==null) return false;
    ListNode pre = null;
    ListNode cur = head;
    ListNode next = head;
    while(cur!=null){
    next = cur.next;
    cur.next = pre;
    pre = cur;
    cur = next;
    }
    return head==pre;
    }
    }


Log in to reply
 

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