I am trying to check if the last node of the linked list points to the head. This code seems to give a positive result for the problem, but also gives a false positive value for the list that contains the node pointing to the non-primary node.
I tried various things, such as checking if the slow node is equal to the head at the returned true point, but this does not seem to work.
public boolean isLinkedToStart(Node head) { if (head == null) { return false; } Node fast = head.next; Node slow = head; while (fast != null && fast.next != null) { if (fast.next.next == slow) { return true; } fast = fast.next.next; slow = slow.next; } return false; }
Any suggestions?
source share