[Linked List Cycle]: input {1,2}, no circle? What does mean?


  • 1
    W

    Input: {1,2}, no cycle
    Output: true
    Expected: false

    I do not understand how {1,2} can be a input
    Isn't the input is a structure?

    class Solution {
    public:
    bool hasCycle(ListNode *head) {
    // IMPORTANT: Please reset any member data you declared, as
    // the same Solution instance will be reused for each test case.
    }
    };


  • 0
    W

    I found the answer myself.
    {1,2} denotes two different nodes. There is no circle here.
    If input is {1,2,3,2}, means there is a circle {2,3,2}.


  • 0
    S

    Could you please format your tags? - is use for combining words, space will treat as tag separator. I think your tag should be linked-list-cycle and input.


  • 4
    Y

    Hi wshaoxuan, actually I am not quite agree with your idea.
    linked list cycle is not identify by its value, is up to ".next".
    Check out the data structure again:

    class ListNode {
          int val;
          ListNode next;
          ListNode(int x) {
              val = x;
              next = null;
          }
     }
    

    for your example {1,2,3,2}, this is not a linked list cycle, 1,2,3,2 are only the values.
    the structure is like 1->2->3->2->null. If "next" of the last node which value is "2" point to the the second node, this means there is a cycle, otherwise, there is no cycle in the linked list.


  • 1
    H
    1. no cycle

    1----->2----->NULL

    1. has cycle

    1----->2------>1 [the address of first '1' is the same with the second '1']

    enter image description here


  • 0
    G

    Well, Input is a structure but instead of representing the link list like as follows

    a -> b -> c->NULL.

    OJ is showing you like {a,b,c}, so you can assume that {1,2} is equivalent to 1->2->NULL.


  • 0
    K
    This post is deleted!

Log in to reply
 

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