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 addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: dummyHead = ListNode() current = dummyHead def _add(d): nonlocal current temp = ListNode(d % 10) current.next = temp current = temp return d // 10 t = 0 while (l1 and l2): t = _add(l1.val + l2.val + t) l1, l2 = l1.next, l2.next l3 = l1 if l1 else l2 while (l3): t = _add(l3.val + t) l3 = l3.next if (t > 0): temp = ListNode(t) current.next = temp current = temp return dummyHead.next
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 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 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 test(): head = ListNode.build(5) print("original list") ListNode.show(head) print("reversed list by single assignment") ListNode.show(reverseSingle(head)) head = ListNode.build(5) print("reversed list by tuple assignment") ListNode.show(reverseTuple(head))
def add_two_number_helper(self, l1: ListNode, l2: ListNode, v: 0) -> ListNode: ''' 相加帮助类 Args: l1: l1链表 l2: l2链表 v: 值 Returns: 链表 ''' if not l1 and not l2: return None if v == 0 else ListNode(v) if l1: v += l1.val l1 = l1.next if l2: v += l2.val l2 = l2.val return ListNode(v % 10, self.add_two_number_helper(l1, l2, v / 10))
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 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 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
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 mergeTwoListsRefine(self, l1: ListNode, l2: ListNode) -> ListNode: if l1 and l2: dummyHead = ListNode(None) current = dummyHead while l1 and l2: if l1.val <= l2.val: current.next = l1 l1 = l1.next else: current.next = l2 l2 = l2.next current = current.next current.next = l1 if l1 else l2 return dummyHead.next return l1 or l2
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if ((l1 or l2) is None): return None if (l1 is None): return l2 if (l2 is None): return l1 dummyNode = currentNode = ListNode(None) while (l1 and l2): if (l1.val <= l2.val): currentNode.next = l1 currentNode = l1 l1 = l1.next else: currentNode.next = l2 currentNode = l2 l2 = l2.next currentNode.next = l1 if l1 else l2 return dummyNode.next
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
t = _add(l3.val + t) l3 = l3.next if (t > 0): temp = ListNode(t) current.next = temp current = temp return dummyHead.next if __name__ == '__main__': # l1 = ListNode.array2ist([9,9,9,9,9,9]) # l2 = ListNode.array2ist([9,9,9,9]) # l1 = ListNode.array2ist([2,4,9]) # l2 = ListNode.array2ist([5,6,4,9]) l1 = ListNode.array2ist([9, 9, 1]) l2 = ListNode.array2ist([1]) print("------------------l1------------------") ListNode.show(l1) print("------------------l2------------------") ListNode.show(l2) solution = Solution() result = solution.addTwoNumbers(l1, l2) # result = solution.addTwoNumbersRecursive(l1, l2) print("----------------result----------------") ListNode.show(result)
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 if __name__ == '__main__': list_node = ListNode() l1 = list_node.create_list_node_array([1, 2, 4]) l2 = list_node.create_list_node_array([1, 3, 4, 5]) solution = Solution() result = solution.merge_two_lists_2(l1, l2) print(result.val) assert result.val == 1
return self.add_two_number_helper(l1, l2, 0) def add_two_number_helper(self, l1: ListNode, l2: ListNode, v: 0) -> ListNode: ''' 相加帮助类 Args: l1: l1链表 l2: l2链表 v: 值 Returns: 链表 ''' if not l1 and not l2: return None if v == 0 else ListNode(v) if l1: v += l1.val l1 = l1.next if l2: v += l2.val l2 = l2.val return ListNode(v % 10, self.add_two_number_helper(l1, l2, v / 10)) if __name__ == '__main__': l1 = ListNode.create_list_node_array([1, 2, 3]) l2 = ListNode.create_list_node_array([2, 3, 4]) solution = Solution() result = solution.add_two_number(l1, l2) print(result)
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
pos = _getLength(head) - n if (pos < 0): return head prev = current = head i = 0 while (current): if (i == pos): if (i == 0): head = head.next break prev.next = current.next break prev, current, i = current, current.next, i + 1 return head if __name__ == '__main__': list = ListNode.array2ist([5,6,4,9]) # list = ListNode.array2ist([1]) solution = Solution() result = solution.removeNthFromEnd(list, 4) ListNode.show(result)
return True else: tempSet.add(head.next) head = head.next return False """ Runtime: 56 ms, faster than 51.53% of Python3 online submissions for Linked List Cycle. Memory Usage: 17.6 MB, less than 75.05% of Python3 online submissions for Linked List Cycle. """ def hasCycleByFastSlowPointer(self, head: ListNode) -> bool: if (not (head and head.next)): return False slow = fast = head while (slow and fast and fast.next): slow = slow.next fast = fast.next.next if (slow is fast): return True return False if __name__ == '__main__': head = ListNode.build(2) solution = Solution() result = solution.hasCycleByFastSlowPointer(head) print(result)
class Solution: def get_intersection_node(self, head_a: ListNode, head_b: ListNode) -> ListNode: ''' 返回交点 Args: head_a: 链表a head_b: 链表b Returns: 交点 ''' if not head_a or not head_b: return None p_A = head_a p_B = head_b while p_A != p_B: p_A = head_b if not p_A else p_A.next p_B = head_a if not p_B else p_B.next return p_A if __name__ == '__main__': arr1 = [4, 1, 8, 4, 5] l1 = ListNode.create_list_node_array(arr1) arr2 = [5, 0, 1, 8, 4, 5] l2 = ListNode.create_list_node_array(arr2) solution = Solution() result = solution.get_intersection_node(l1, l2) print(result) assert result == None
def test_one_element(): l1 = ListNode(1) assert list_is_sorted(l1)
def _add(d): nonlocal current temp = ListNode(d % 10) current.next = temp current = temp return d // 10
if node_list[i] != node_list[j]: return False i += 1 j -= 1 return True def is_palindrome(self, head: ListNode) -> bool: ''' 返回交点 Args: head: 节点head Returns: 布尔值 ''' node_list = [] if not head: return True while head: node_list.append(head.val) head = head.next return self.is_palindrome_list(node_list) if __name__ == '__main__': arr = [1, 2, 3, 3, 2, 1] list_node = ListNode() head = list_node.create_list_node_array(arr) solution = Solution() result = solution.is_palindrome(head) print(result)
current = dummyHead while l1 and l2: if l1.val <= l2.val: current.next = l1 l1 = l1.next else: current.next = l2 l2 = l2.next current = current.next current.next = l1 if l1 else l2 return dummyHead.next return l1 or l2 if __name__ == '__main__': list_1 = ListNode.buildSortedList(2) list_2 = ListNode.buildSortedList(3) ListNode.show(list_1) ListNode.show(list_2) solution = Solution() merged = solution.mergeTwoLists(list_1, list_2) ListNode.show(merged) # list_1 = ListNode.buildEmptyList() # list_2 = ListNode.buildSortedList(1) # ListNode.show(list_1) # ListNode.show(list_2)