while current: nextNode = current.nextnode current.nextnode = prevNode prevNode = current current = nextNode return a = Node(1) b = Node(2) c = Node(3) d = Node(4) a.nextnode = b b.nextnode = c c.nextnode = d print(a.nextnode.value) print(b.nextnode.value) print(c.nextnode.value) # Reversing the Linked List print("##################") reverse(a) print("##################") print(d.nextnode.value) print(c.nextnode.value) print(b.nextnode.value)
def nth_to_last_node(n, head): slow = head fast = head for _ in range(n): if not slow.nextnode: raise LookupError('Error: n is larger than the linked List') fast = fast.nextnode while fast.nextnode: slow = slow.nextnode fast = fast.nextnode return slow a = Node(1) b = Node(2) c = Node(3) d = Node(4) e = Node(5) a.nextnode = b b.nextnode = c c.nextnode = d d.nextnode = e target_node = nth_to_last_node(3, a) print(target_node.value)
while marker1 != None and marker2.nextnode != None: marker1 = marker1.nextnode marker2 = marker2.nextnode.nextnode #check if the markers have crossed if marker1 == marker2: return True # Case where the marker ahead reaches the end of the list return False a = Node(1) b = Node(2) c = Node(3) a.nextnode = b b.nextnode = c c.nextnode = a # Cycle List x = Node('ro') y = Node('mo') z = Node('to') x.nextnode = y y.nextnode = z print(cycle_check(a)) # circular print(cycle_check(x)) # not circular