public bool HasCycle(ListNode_141 head) { // To judge the pos is less than the length of ListNode bool hasCycle = false; List <ListNode_141> allNodeList = new List <ListNode_141>(); ListNode_141 currentNode = head; while (currentNode != null && currentNode.next != null) { ListNode_141 nextNode = currentNode.next; int pos = allNodeList.FindIndex(x => x.Equals(nextNode)); if (pos > -1) { hasCycle = true; break; } allNodeList.Add(currentNode); currentNode = currentNode.next; } return(hasCycle); }
public ListNode_141(int x) { val = x; next = null; }