/* * * @author : vgade */ public bool DetectCycle(ListNode inputHead) { ListNode prevNode = inputHead; inputHead = inputHead.next.next; while (inputHead != null) { if (inputHead.Equals(prevNode)) { prevNode = prevNode.next; } else { return(true); } if (inputHead.next != null) { inputHead = inputHead.next.next; } else { inputHead = null; } } return(false); }
public ListNode DetectCycle(ListNode head) { ListNode node1 = head; ListNode node2 = head; bool hasCycle = false; while (node2 != null && node2.next != null) { node1 = node1.next; node2 = node2.next.next; if (node1.Equals(node2)) { hasCycle = true; break; } } if (hasCycle) { node1 = head; while (!node1.Equals(node2)) { node1 = node1.next; node2 = node2.next; } return(node1); } return(null); }
public void TestSimple() { Assert.IsTrue(ListNode.Equals(ListNode.Create(new int[] { 1, 2, 3, 4, 5 }), util.MergeTwoLists(ListNode.Create(new int[] { 1, 2, 3 }), ListNode.Create(new int[] { 4, 5 })))); Assert.IsTrue(ListNode.Equals(ListNode.Create(new int[] { 1 }), util.MergeTwoLists(null, ListNode.Create(new int[] { 1 })))); }
void showFull() { ListNode start = current; if (current.getName().Equals("nothing")) { start = current.getNext(); } ListNode tempNode = start; int i = 0; do { if (tempNode.getName().Equals("nothing")) { tempNode = tempNode.getNext(); continue; } Image image = invDisplay[i].GetComponent <Image>(); image.sprite = tempNode.getVal().GetComponent <SpriteRenderer>().sprite; var tempColor = image.color; tempColor.a = 255f; image.color = tempColor; i++; tempNode = tempNode.getNext(); }while (!tempNode.Equals(start)); var temp = invText.GetComponent <Text>().color; temp.a = 255f; invText.GetComponent <Text>().color = temp; temp = panel.GetComponent <Image>().color; temp.a = 200f; panel.GetComponent <Image>().color = temp; }
public void TestSimple() { Assert.IsTrue( ListNode.Equals( ListNode.Create(new int[] { 2 }), this.util.RemoveNthFromEnd(ListNode.Create(new int[] { 1, 2 }), 2))); Assert.IsTrue( ListNode.Equals( ListNode.Create(new int[] { 1, 2, 4, 5 }), this.util.RemoveNthFromEnd(ListNode.Create(new int[] { 1, 2, 3, 4, 5 }), 3))); }
public void ReverseList_FromLeetCode_ShouldReturnReversedList() { var node = new ListNode(1); node.next = new ListNode(2); node.next.next = new ListNode(3); var expectedResult = new ListNode(3); expectedResult.next = new ListNode(2); expectedResult.next.next = new ListNode(1); var result = ReverseListImplementation.ReverseList(node); Assert.True(result.Equals(expectedResult) && expectedResult.Equals(result)); }
public void Delete(ListNode listNode) { if (_firstNode != null) { if (listNode != null) { if (_firstNode.Equals(listNode)) { _firstNode = _firstNode.Next; } else { _firstNode.Delete(listNode); } } } }
// Does linked list have a cyclical loop? public bool HasCycle(ListNode head) { ListNode newListHead = null; ListNode newListTail = null; // Build a new list while iterating the existing list. // If a duplicate node is detected, a cycle exists. ListNode node = head; while (node != null) { // Cache next pointer because we will change it. ListNode next = node.next; // Iterate the previous entries in the new list looking for duplicates. for (ListNode n = newListHead; n != null; n = n.next) { // If we find a duplicate node, list has cycled, // so we can return immediately. // Uses Object.Equals() for node equality. if (n.Equals(node)) { return(true); } } // Add node to new list. // Builds new pointer links to avoid any potential loops. if (newListTail != null) { newListTail.next = node; } newListTail = node; if (newListHead == null) { newListHead = node; } node.next = null; node = next; } // If we get here, the input list // did not have any cycles and ended normally. return(false); }
public override bool Equals(object obj) { var otherNode = obj as ListNode; if (Val != otherNode.Val) { return(false); } if (Next == null) { return(otherNode.Next == null); } if (otherNode.Next == null) { return(Next == null); } return(Next.Equals(otherNode.Next)); }
public static ListNode GetInterSectionModelBySameLength(ListNode headA, ListNode headB) { int lenA = GetLength(headA), lenB = GetLength(headB); // move headA and headB to the same start point while (lenA > lenB) { headA = headA.Next; lenA--; } while (lenA < lenB) { headB = headB.Next; lenB--; } // find the intersection until end while (!ListNode.Equals(headA, headB)) { headA = headA.Next; headB = headB.Next; } return(headA); }
public bool HasCycle(ListNode head) { ListNode current = head, runner = head; while (current != null && runner != null) { if (null != runner.next) { runner = runner.next.next; } else { break; } if ((null != runner && null != current) && runner.Equals(current)) { return(true); } current = current.next; } return(false); }