def partition (LList, x): this_node = LList.get_root() smaller = LinkedList() larger = LinkedList() while this_node: if this_node.get_data() < x: smaller.add_tail(this_node.get_data()) else: larger.add_tail(this_node.get_data()) this_node = this_node.get_next() smaller.add_node(larger.get_root()) return smaller
#!/usr/bin/python from LinkedList import LinkedList def detectLoop(l): if l.get_size()<=0: return False slow = fast = l.get_root() while (slow and fast and fast.get_next()): slow = slow.get_next() fast = fast.get_next().get_next() if slow == fast: fast = l.get_root() while (slow and fast): slow = slow.get_next() fast = fast.get_next() if slow == fast: return slow list = LinkedList() list.add_tail('A') list.add_tail('B') list.add_tail('C') list.add_tail('D') list.add_tail('E') list.get_root().get_next().get_next().get_next().get_next().set_next(list.get_root().get_next().get_next()) point = detectLoop(list) print point.get_data()