def test_merge_two_sorted_lists(self): head = merge_k_lists( [generate_linked_list([1, 4, 5]).get_head(), generate_linked_list([1, 3, 4]).get_head(), generate_linked_list([2, 6]).get_head()]) result = LinkedList(head=head) self.assertListEqual([1, 1, 2, 3, 4, 4, 5, 6], result.to_list())
def test_partition_list(self): linked_list_1 = generate_linked_list([2, 4, 3]) linked_list_2 = generate_linked_list([5, 6, 4]) head = add_two_numbers(linked_list_1.get_head(), linked_list_2.get_head()) linked_list_3 = LinkedList(singly=True, head=head) self.assertListEqual([7, 0, 8], linked_list_3.to_list())
def test_copy_list_with_random_pointer(self): node_1 = Node(1) node_2 = Node(2) node_1.next = node_2 node_1.random = node_2 node_2.random = node_2 head_copied = copy_random_list(node_1) linked_list = LinkedList(head=head_copied) self.assertListEqual([1, 2], linked_list.to_list()) self.assertIs(head_copied.next, head_copied.random) self.assertIs(head_copied.next, head_copied.next.random)
def _create_lists(start_1: int, hop_1: int, max_1: int, start_2: int, hop_2: int, max_2: int) -> Tuple[Node, Node]: ll_1: LinkedList = LinkedList(Node(start_1)) ll_2: LinkedList = LinkedList(Node(start_2)) i: int = start_1 + hop_1 j: int = start_2 + hop_2 while i < max_1: ll_1.append(Node(i)) i += hop_1 while j < max_2: ll_2.append(Node(j)) i += hop_2 return ll_1.head, ll_2.head
def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if not l1: return l2 if not l2: return l1 head = None if l1.val < l2.val: head = l1 else: head = l2 target = LinkedList() while True: if not l1 and not l2: break if not l1: target.append(l2.val) l2 = l2.next continue if not l2: target.append(l1.val) l1 = l1.next continue if l1.val < l2.val: target.append(l1.val) l1 = l1.next else: target.append(l2.val) l2 = l2.next return target.head
def test_sum_lists_reversed(self): ll_1 = LinkedList(Node(9)) ll_2 = LinkedList(Node(7)) ll_1.append(Node(1)) ll_1.append(Node(3)) ll_1.append(Node(2)) ll_1.append(Node(1)) ll_2.append(Node(8)) ll_2.append(Node(7)) sum_head = sum_lists_reverse(ll_1.head, ll_2.head) assert sum_head.data == 9 assert sum_head.next.data == 2 assert sum_head.next.next.data == 1 assert sum_head.next.next.next.data == 0 assert sum_head.next.next.next.next.data == 8
def head(self): """ Initialize the linked list. """ head = Node(5) linked_list = LinkedList(head) linked_list.append(Node(4)) linked_list.append(Node(3)) linked_list.append(Node(2)) linked_list.append(Node(1)) return head
def sum_lists(ll_a, ll_b): n1, n2 = ll_a.head, ll_b.head ll = LinkedList() carry = 0 while n1 or n2: result = carry if n1: result += n1.value n1 = n1.next if n2: result += n2.value n2 = n2.next ll.append(result % 10) carry = result // 10 if carry: ll.append(carry) return ll
nxt_val = l1.val + l2.val + over over, nxt_val = nxt_val / 10, nxt_val % 10 curr.next = ListNode(nxt_val) curr = curr.next l1, l2 = l1.next, l2.next if over > 0: curr.next = ListNode(over) return res.next if res.next else None if __name__ == '__main__': # l1 = 2 -> 4 -> 3 # l2 = 5 -> 6 -> 4 # ans = 7 -> 0 -> 8 l1 = LinkedList() l1.toLinkedList([2,4,3]) l2 = LinkedList() l2.toLinkedList([5,6,4]) l1.toLinkedList([3,4]) l2.toLinkedList([8,6,0,0,5]) resNode = Solution().addTwoNumbers(l1.head, l2.head) res = LinkedList(resNode) res.printLL() l1.toLinkedList([1,8]) l2.toLinkedList([0]) resNode = Solution().addTwoNumbers(l1.head, l2.head) res = LinkedList(resNode) res.printLL()
def test_merge_two_sorted_lists(self): l1 = generate_linked_list([1, 2, 4]) l2 = generate_linked_list([1, 3, 4]) head = merge_two_lists(l1.get_head(), l2.get_head()) l3 = LinkedList(head=head) self.assertListEqual([1, 1, 2, 3, 4, 4], l3.to_list())
raise Exception('LL is Empty') node1 = ll.head node2 = ll.head tmp = node2 while node1 is not None: tmp = node2 node2 = node2.next node1 = node1.next.next if node1.next is not None else None tmp.next = None # node2 中间节点 back = node2 front = ll.head return front, back ll = LinkedList() ll.add_first(2) ll.add_first(3) ll.add_first(4) ll.add_first(5) ll.print_list() practice_work1(ll.head.next.next) practice_work2(ll) # 起一个循环链表 a = Node(1) b = Node(2) c = Node(3)
from utils import LinkedList def search(ll_list, key): tmp = ll_list.head while tmp is not None: if tmp.data == key: return True tmp = tmp.next return False if __name__ == '__main__': linked_list = LinkedList() linked_list.append(3) linked_list.push(4) linked_list.push(1) linked_list.append(5) print(search(linked_list, 43))
from utils import ListNode, LinkedList class Solution(object): def removeNthFromEnd(self, head, n): first, second = head, None count = 0 while first: if count == n and not second: second = head elif count > n: second = second.next first = first.next count += 1 if not second: return head.next second.next = second.next.next return head if __name__ == '__main__': ll = LinkedList() ll.toLinkedList([1,2,3,4,5]) res = Solution().removeNthFromEnd(ll.head, 2) resLL = LinkedList(res) resLL.printLL()
def head(self): """ Initialize linked list. """ head = Node(1) linked_list = LinkedList(head) linked_list.append(Node('hello')) linked_list.append(Node('why')) linked_list.append(Node('34')) linked_list.append(Node('0293148')) linked_list.append(Node(2034820)) linked_list.append(Node(True)) linked_list.append(Node(False)) return head
def assert_operation(val_list, result_list): linked_list = LinkedList(singly=True) linked_list.append_val_list(val_list) delete_duplicates(linked_list.get_head()) self.assertListEqual(result_list, linked_list.to_list())
def sum_lists(ll_a, ll_b): n1, n2 = ll_a.head, ll_b.head ll = LinkedList() carry = 0 while n1 or n2: result = carry if n1: result += n1.value n1 = n1.next if n2: result += n2.value n2 = n2.next ll.append(result % 10) carry = result // 10 if carry: ll.append(carry) return ll ll_a = LinkedList() ll_a.append_multiple([6, 1, 7]) ll_b = LinkedList() ll_b.append_multiple([2, 9, 5]) ll_a.display() ll_b.display() ll_sum = sum_lists(ll_a, ll_b) ll_sum.display()
from utils import LinkedList def delete_middle_node(node): node.value = node.next.value node.next = node.next.next ll = LinkedList() ll.append_multiple([1, 2, 3, 4]) middle_node = ll.append(5) ll.append_multiple([6, 7, 8, 9]) ll.display() delete_middle_node(middle_node) ll.display()
from utils import LinkedList def kth_to_last(ll, k): if k <= 0: raise (AttributeError('Set k to a value greater than 0')) current = runner = ll.head for i in range(k): runner = runner.next while runner: current = current.next runner = runner.next return current.value ll = LinkedList() ll.append_multiple([1, 2, 3, 4, 5, 6, 7, 8, 9]) kth_to_last = kth_to_last(ll, 2) print(kth_to_last)
from utils import LinkedList def palindrome_runner(ll): runner = current = ll.head stack = [] while runner and runner.next: stack.append(current.value) current = current.next runner = runner.next.next if runner: current = current.next while current: top = stack.pop() if top != current.value: return False current = current.next return True ll = LinkedList() ll.append_multiple([1, 2, 3, 4, 3, 2, 1]) ll.display() result = palindrome_runner(ll) print(result)
from utils import LinkedList def partition(ll, x): current = ll.tail = ll.head while current: nextNode = current.next current.next = None if current.value < x: current.next = ll.head ll.head = current else: ll.tail.next = current ll.tail = current current = nextNode # Error check in case all nodes are less than x if ll.tail.next is not None: ll.tail.next = None return ll ll = LinkedList() n = ll.append(55) ll.append_multiple([82, 95, 20, 47, 37, 11, 42, 91, 6, 74]) ll.display() print('Patition created at {}'.format(n.value)) ll = partition(ll, n.value) ll.display()
if not l1: target.append(l2.val) l2 = l2.next continue if not l2: target.append(l1.val) l1 = l1.next continue if l1.val < l2.val: target.append(l1.val) l1 = l1.next else: target.append(l2.val) l2 = l2.next return target.head if __name__ == '__main__': list1 = LinkedList().buildFromList([1, 3, 5]) list2 = LinkedList().buildFromList([2, 4, 6]) # LinkedList.dump(list1) # LinkedList.dump(list2) ret = Solution().mergeTwoLists(list1.head, list2.head) LinkedList.dump(ret)
from utils import LinkedList def reverse(ll): previous = None current = ll.head next_node = current.next while current: current.next = previous previous = current current = next_node if next_node: next_node = next_node.next ll.head = previous return ll ll = LinkedList() ll.add_multiple([1, 2, 3, 4, 5, 6, 7, 8, 9]) ll.display() ll = reverse(ll) ll.display()
diff = longer.length - shorter.length short, long = shorter.head, longer.head for i in range(diff): long = long.next while short is not long: short = short.next long = long.next return long.value ll_1 = LinkedList() ll_2 = LinkedList() ll_1.append_multiple([2, 8, 4, 9, 2]) ll_2.append_multiple([3, 4, 1]) node = Node(5) ll_1.append(node) ll_2.append(node) node = Node(1) ll_1.append(node) ll_2.append(node) ll_1.display() ll_2.display() result = intersection(ll_1, ll_2) print(result)
合并l1 和 l2 2个有序链表 :param l1: :param l2: :return: ''' if not l1 or not l2: return l1 or l2 if l1.val > l2.val: l2.next = merge_two_sorted_linked_list1(l1, l2.next) return l2 else: l1.next = merge_two_sorted_linked_list1(l1.next, l2) return l1 l1 = LinkedList() l1.add_last(1) l1.add_last(5) l1.add_last(7) l1.print_list() l2 = LinkedList() l2.add_last(2) l2.add_last(4) l2.add_last(6) merge_two_sorted_linked_list1(l1.head.next, l2.head.next) merge_two_sorted_linked_list(l1.head.next, l2.head.next) # 两个链表的交集 # 写一个可以找到两个链表交集开始节点的程序
def loop_detection(ll): fast = slow = ll.head while fast and fast.next: print(slow.value, fast.value) fast = fast.next.next slow = slow.next if fast == slow: break print(slow.value, fast.value) if fast is None or fast.next is None: return None print('---') slow = ll.head while fast != slow: print(slow.value, fast.value) fast = fast.next slow = slow.next print(slow.value, fast.value) print(fast.value) ll = LinkedList() ll.append_multiple([1, 2, 3, 4]) node = Node(5) ll.append_multiple([node, Node(6), Node(7), Node(8), Node(9), node]) loop_detection(ll)
def assert_operation(val_list, x, result_list): linked_list = LinkedList(singly=True) linked_list.append_val_list(val_list) head = partition(linked_list.get_head(), x) linked_list.set_head(head) self.assertListEqual(result_list, linked_list.to_list())
# 反转一个链表 def reverseLinkedList(ll): if not ll: raise ValueError("ll is Empty.") pre = None curr = ll while curr is not None: tmp = curr.next # 记录当前节点的之后节点 curr.next = pre # 当前节点连接在pre上 pre = curr # 将pre节点移到curr上 curr = tmp # 移动curr节点 return pre ll = LinkedList() ll.add_first(4) ll.add_first(5) ll.add_first(3) ll.add_first(7) ll.add_first(9) ll.print_list() a = reverseLinkedList(ll.head.next) ll.head.next = a ll.print_list() # 反转一个链表2 从m到n之间进行反转 def reverseLinkedList2(ll, m, n): if m > n: