def palindrome(alist): head = alist.head if head == None: return head #1.find mid point fast = head.next slow = head while fast is not None and fast.next is not None: fast = fast.next.next slow = slow.next #2.split into two linklist head2 = Node() if fast == None: head2.next = slow.next slow.next = None else: head2.next = slow.next.next slow.next = None #3.reverse second linklist pre = None cur = head2.next while cur: temp = cur.next cur.next = pre pre = cur cur = temp # judging if it is palindrome while pre.next is not None: if pre.value != head.next.value: return False else: pre = pre.next head = head.next return True
def LinkListtoBST(head): if head is None: return None dummy = Node(0) dummy.next = head fast = dummy slow = dummy slow_tail = dummy while fast is not None and fast.next is not None: fast = fast.next.next slow_tail = slow slow = slow.next slow_tail.next = None node = Nd(slow.value) node._left = LinkListtoBST(dummy.next) node._right = LinkListtoBST(slow.next) return node
slow_tail = dummy while fast is not None and fast.next is not None: fast = fast.next.next slow_tail = slow slow = slow.next slow_tail.next = None node = Nd(slow.value) node._left = LinkListtoBST(dummy.next) node._right = LinkListtoBST(slow.next) return node if __name__ == '__main__': node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node5 = Node(5) node6 = Node(6) node1.next = node2 node2.next = node3 node3.next = node4 node4.next = node5 node5.next = node6 bst = BinarySearchTree() result = LinkListtoBST(node1) bst._root = result bst.print_inorder()
if node.next is None: return node head = reverse_recursion(node.next) node.next.next = node node.next = None return head if __name__ == '__main__': ll = LinkList() ll.add_last(1) ll.add_last(2) ll.add_last(3) ll.add_last(4) ll.print_list() print() reverse_l(ll) ll.print_list() print() node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node1.next = node2 node2.next = node3 node3.next = node4 result = reverse_recursion(node1) l2 = LinkList() l2.head.next = result l2.print_list()
ll_a.add_last(1) ll_a.add_last(3) ll_a.add_last(5) ll_a.add_last(7) ll_b = LinkList() ll_b.add_last(2) ll_b.add_last(4) ll_b.add_last(5) ll_b.add_last(8) ll_b.add_last(10) ll_b.add_last(12) node1 = Node(1) node2 = Node(3) ndoe3 = Node(5) node1.next = node2 node2.next = ndoe3 node_a = Node(2) node_b = Node(4) node_c = Node(5) node_d = Node(8) node_a.next = node_b node_b.next = node_c node_c.next = node_d #result = merge_list(node1, node_a) #ll = LinkList() #ll.head.next = result #ll.print_list() #print()
def find_cycle(llist): return find_cycle_helper(llist.head) def find_cycle_helper(head): if head is None: return False fast = head slow = head while fast is not None and fast.next is not None: fast = fast.next.next slow = slow.next if slow == fast: return True return False if __name__ == '__main__': node1 = Node(1) node2 = Node(2) node3 = Node(3) node1.next = node2 node2.next = node3 node3.next = node1 result = find_cycle_helper(node1) print(result)
b = b.next if b else head_a return a.value if __name__ == '__main__': a1 = Node(1) a2 = Node(3) a3 = Node(5) c1 = Node(9) c2 = Node(10) c3 = Node(11) b1 = Node(2) b2 = Node(4) b3 = Node(6) b4 = Node(8) a1.next = a2 a2.next = a3 a3.next = c1 c1.next = c2 c2.next = c3 b1.next = b2 b2.next = b3 b3.next = b4 b4.next = c1 result = find_same(a1, b1) print(result) result2 = find_same2(a1, b1) print(result2)