def add_two_number(self, l1: ListNode, l2: ListNode) -> ListNode: ''' 两个链表相加 Args: l1: 链表l1 l2: 链表l2 Returns: 链表的和 ''' node = ListNode(-1) head = node carry = 0 while not l1 or not l2: num1 = 0 if not l1 else l1.val num2 = 0 if not l2 else l2.val total = num1 + num2 + carry node.next = ListNode(total % 10) node = node.next carry = total // 10 if not l1: l1 = l1.next if not l2: l2 = l2.next if carry > 0: node.next = ListNode(carry) return head.next
def test_first_list_bigger(): l1 = ListNode(5) l1.next = ListNode(10) l1.next.next = ListNode(200) l2 = ListNode(100) l2.next = ListNode(150) l3 = merge(l1, l2) assert list_is_sorted(l3)
def test_simple_example(): l1 = ListNode(1) l1.next = ListNode(2) l1.next.next = ListNode(4) l2 = ListNode(1) l2.next = ListNode(3) l2.next.next = ListNode(4) l3 = merge(l1, l2) assert list_is_sorted(l3)
def create_rand_sorted_list(n: int) -> ListNode: node = ListNode(random.randint(0, 10)) head = node for i in range(n - 1): node.next = ListNode(random.randint(node.value, node.value + 10)) node = node.next return head
def test_multiple_elements(): l1 = ListNode(2) l1.next = ListNode(5) l1.next.next = ListNode(10000) assert list_is_sorted(l1) l1.next.next.next = ListNode(6) assert not list_is_sorted(l1)
def merge_two_lists_2(self, l1: ListNode, l2: ListNode) -> ListNode: ''' 合并两个链表 Args: l1: l1链表 l2: l2链表 Returns: 合并后的链表 ''' if not l1: return l2 if not l2: return l1 if l1.val <= l2.val: l1.next = self.merge_two_lists_2(l1.next, l2) return l1 else: l2.next = self.merge_two_lists_2(l1, l2.next) return l2
def merge_two_lists(self, l1: ListNode, l2: ListNode) -> ListNode: ''' 合并两个链表 Args: l1: l1链表 l2: l2链表 Returns: 合并后的链表 ''' node = ListNode(-1) head = node while l1 and l2: if l1.val <= l2.val: node.next = l1 l1 = l1.next else: node.next = l2 l2 = l2.next node = node.next if not l1: node.next = l2 if not l2: node.next = l1 return head.next
def detect_cycle_2(self, head: ListNode) -> ListNode: ''' 头结点 Args: head: 头结点位置 Returns: 环中头结点位置 ''' if not head or not head.next: return None node = ListNode(-1) node.next = head cur = node list_node_set = set() while cur.next: if cur.next in list_node_set: return cur.next cur = cur.next list_node_set.add(cur) return None
def remove_nth_from_end(self, head: ListNode, n: int) -> ListNode: ''' 删除倒数第n个点 Args: head: 头节点 n: 倒数n个数 Returns: 删除后的结果 ''' if head == None or head.next == None: return None dummy = ListNode(-1) dummy.next = head node1 = dummy node2 = dummy for i in range(n): node2 = node2.next while node2.next: node1 = node1.next node2 = node2.next node1.next = node1.next.next return dummy.next
Args: head: 头结点 Returns: 中间结点 ''' if not head or not head.next: return head count = 0 temp = head while temp: count += 1 temp = temp.next for i in range(count // 2): head = head.next return head if __name__ == '__main__': node1 = ListNode(1) node2 = ListNode(2) node3 = ListNode(3) node4 = ListNode(4) node5 = ListNode(5) node1.next = node2 node2.next = node3 node3.next = node4 node4.next = node5 solution = Solution() node = solution.middle_linked_list(node1) assert node.val == 3