#!/usr/bin/python """ Write a function to find the middle node of a singly-linked list. http://nbl.cewit.stonybrook.edu/algowiki/index.php/Data-structures-TADM2E-2 """ from linked_list import randlinkedlist ll = randlinkedlist(size=9) print "The linked list: {0}".format(ll) slow, fast = ll.head, ll.head while fast: if fast.next is None: break slow, fast = slow.next, fast.next.next print "The middle of the linked list: {0}".format(slow.value)
#!/usr/bin/python # vim: foldlevel=0 """ Reverse a linked list in groups of size k. http://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/ """ from linked_list import randlinkedlist ll = randlinkedlist() print "The linked list: {0}".format(ll) def reverse_linked_list(head, k): if not head: return node = head prev, next = None, None count = 0 while node and count < k: next = node.next node.next = prev prev, node = node, next count += 1 head.next = reverse_linked_list(node, k) return prev ll.head = reverse_linked_list(ll.head, 3) print "The reversed linked list (iterative): {0}".format(ll)
from linked_list import randlinkedlist def solution(node): if not node or not node.next: return next = node.next node.value = next.value node.next = next.next if __name__ == "__main__": def _print(head): cur = head while cur: print cur.value cur = cur.next ll = randlinkedlist() print "Original linked list: " _print(ll.head) node = ll.head for i in range(5): node = node.next solution(node) print "New linked list: " _print(ll.head)
#!/usr/bin/python """ Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop. http://nbl.cewit.stonybrook.edu/algowiki/index.php/Data-structures-TADM2E-2 """ from linked_list import randlinkedlist ll = randlinkedlist(size=20) print str(ll) def find_cycle(llist): slow, fast = llist.head, llist.head while fast: if fast.next is None: break if fast.next == slow: return True slow, fast = slow.next, fast.next.next return False print find_cycle(ll) # should return False cur = ll.head cycle_start = None for i in range(ll.count - 1): if i == 4: cycle_start = cur
def solution3(head): ''' Same as solution2. ''' cur = head while cur: prev, runner = cur, cur.next while runner: if runner.value != cur.value: prev.next = runner prev = runner runner = runner.next prev.next = None cur = cur.next if __name__ == "__main__": def _print(head): cur = head while cur: print cur.value cur = cur.next ll = randlinkedlist(max=10) print "Original linked list: " _print(ll.head) solution(ll.head) print "Cleaned up linked list: " _print(ll.head)
# Now that the linked lists are of equal size, add them head, carry = add_forward(head1, head2) if carry != 0: node = Node(carry) node.next, head = head, node # insert at front return head if __name__ == "__main__": def _print(head): cur = head while cur: print cur.value cur = cur.next ll1 = randlinkedlist(3, max=9) ll2 = randlinkedlist(5, max=9) print "Linked list 1:" _print(ll1.head) print "Linked list 2:" _print(ll2.head) print "Sum of linked lists (reversed):" _print(solution1(ll1.head, ll2.head)) print "Sum of linked lists (not reversed):" _print(solution2(ll1.head, ll2.head))
head2 = pad_with_zeros(head2, len1-len2) # Now that the linked lists are of equal size, add them head, carry = add_forward(head1, head2) if carry != 0: node = Node(carry) node.next, head = head, node # insert at front return head if __name__ == "__main__": def _print(head): cur = head while cur: print cur.value cur = cur.next ll1 = randlinkedlist(3, max=9) ll2 = randlinkedlist(5, max=9) print "Linked list 1:" _print(ll1.head) print "Linked list 2:" _print(ll2.head) print "Sum of linked lists (reversed):" _print(solution1(ll1.head, ll2.head)) print "Sum of linked lists (not reversed):" _print(solution2(ll1.head, ll2.head))