0

I am trying to implement the boolean method taking a parameter of ListNode list then the method will return true if the list meets null. I think I can do this part, but the problem is for the false statement. If the nodes points back to previous node then it should return false. For example, 2 , 4, 5, null --> true 2 , 4, 5, (points back to previous node) 4, 5, 4, 5, etc

the below code is what i did so far..

public static <T> boolean terminates(ListNode<T> list) {
    // TODO: Part of this assignment is to implement this correctly.
    if(list == null) {
        return true;
    }
    else {
        while(list != null) {
            list = list.next;
        }

        return true;
    }
}
ernest_k
  • 44,416
  • 5
  • 53
  • 99
Sangmin Choi
  • 33
  • 1
  • 5
  • @ernest_k If you don't know the number of nodes in the list ahead of time then that would not work. In this case, a single node is passed in with a pointer or reference to the next, not a whole list. – Domenick Dec 06 '18 at 06:02
  • 1
    Possible duplicate of [How to detect a loop in a linked list?](https://stackoverflow.com/questions/2663115/how-to-detect-a-loop-in-a-linked-list) – Oleg Estekhin Dec 06 '18 at 06:14
  • this helps me a lot !!!!! Thank you all – Sangmin Choi Dec 06 '18 at 17:16

0 Answers0