def get_node_list(list_): head = ListNode(-1) cur = head for item in list_: cur.next = ListNode(item) cur = cur.next return head.next
def get_rand_node_list(length=_defaultLen, min_val=_min_val, max_val=_max_val): head = ListNode(-1) cur = head for i in range(length): cur.next = ListNode(random.randint(min_val, max_val)) cur = cur.next return head.next
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: if l1.val == 0: return l2 if l2.val == 0: return l1 stack1, stack2 = [], [] while l1: stack1.append(l1.val) l1 = l1.next while l2: stack2.append(l2.val) l2 = l2.next carry = 0 node = None while stack1 or stack2 or carry != 0: a = stack1.pop() if stack1 else 0 b = stack2.pop() if stack2 else 0 res = a + b + carry carry = res // 10 res %= 10 cur = ListNode(res) cur.next = node node = cur return node
def get_sorted_node_list(length=_defaultLen, min_val=_min_val, max_val=_max_val): sorted_list = [random.randint(min_val, max_val) for _ in range(length)] sorted_list.sort() head = ListNode(-1) cur = head for v in sorted_list: cur.next = ListNode(v) cur = cur.next return head.next
def swapPairs(self, head: ListNode) -> ListNode: cur = head tmp_head = ListNode(-1) tmp_head.next = head pre = tmp_head while cur and cur.next: pre.next = cur.next pre = cur right = cur.next # cur, cur.next, right.next = right.next, right.next, cur cur.next, right.next, cur = right.next, cur, right.next return tmp_head.next
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: my_head = ListNode(0) my_head.next = head next_node = my_head tmp_list = [] while next_node: tmp_list.append(next_node) if len(tmp_list) > n + 1: tmp_list.pop(0) next_node = next_node.next tmp_list[0].next = tmp_list[1].next return my_head.next
def removeNthFromEnd1(self, head: ListNode, n: int) -> ListNode: my_head = ListNode(0) my_head.next = head next_node = my_head # 使用指针加固定长度数组 模拟队列 queue_len = n + 1 tmp_list = [None] * queue_len cur = 0 while next_node: tmp_list[cur % queue_len] = next_node next_node = next_node.next cur += 1 tmp_list[cur % queue_len].next = tmp_list[(cur + 1) % queue_len].next return my_head.next
def reverseKGroup(self, head: ListNode, k: int) -> ListNode: my_head = ListNode(-1) my_head.next = head pre_head = my_head end = head while end: # 获取结束节点 count = 1 while count < k and end: count += 1 end = end.next # 如果节点长度不够 就可以结束了 if count < k or not end: break start = pre_head.next # head -> 1 stop = end.next # k+1的节点 pre_head.next = end # head -> end pre = start tmp_node = start.next # 第二个节点开始 指向 前一个节点 while tmp_node is not stop: tmp_next = tmp_node.next # 记录下一个节点 tmp_node.next = pre # 指向前一个节点 pre = tmp_node # 记录当前节点 tmp_node = tmp_next # 下一个节点 # 交换首尾连接处 start.next = stop pre_head = start end = stop return my_head.next
def deleteDuplicates(self, head: ListNode) -> ListNode or None: if not head: return None my_head = ListNode(0) my_head.next = head prev = my_head cur = head dup_val = None while cur: if cur.val == dup_val: cur = cur.next continue if cur.next and cur.val == cur.next.val: dup_val = cur.val cur = cur.next.next continue prev.next = cur prev = cur cur = cur.next prev.next = None return my_head.next
next_node = next_node.next tmp_list[0].next = tmp_list[1].next return my_head.next def removeNthFromEnd1(self, head: ListNode, n: int) -> ListNode: my_head = ListNode(0) my_head.next = head next_node = my_head # 使用指针加固定长度数组 模拟队列 queue_len = n + 1 tmp_list = [None] * queue_len cur = 0 while next_node: tmp_list[cur % queue_len] = next_node next_node = next_node.next cur += 1 tmp_list[cur % queue_len].next = tmp_list[(cur + 1) % queue_len].next return my_head.next if __name__ == '__main__': node1 = ListNode(1) node2 = ListNode(2) node1.next = node2 print(node1) s = Solution() res = s.removeNthFromEnd1(node1, 2) print(res)
break left_son = 2 * smallest + 1 right_son = 2 * smallest + 2 def heap_sort(arr): heapify(arr) length = len(arr) for i in range(length - 1): arr[length - i - 1], arr[0] = arr[0], arr[length - i - 1] heapify_1(arr, length - i - 1, 0) if __name__ == '__main__': node1 = ListNode(1) node2 = ListNode(4) node3 = ListNode(5) node1.next = node2 node2.next = node3 arr0 = [node1] node1 = ListNode(1) node2 = ListNode(3) node3 = ListNode(4) node1.next = node2 node2.next = node3 arr0.append(node1) node1 = ListNode(2) node2 = ListNode(6)
return l1 if l1.val > l2.val: l1, l2 = l2, l1 head = l1 pre = None while l1 and l2: while l1 and l1.val <= l2.val: pre = l1 l1 = l1.next pre.next = l2 l1, l2 = l2, l1 return head if __name__ == '__main__': node1_1 = ListNode(1) node1_2 = ListNode(1) node1_1.next = node1_2 node2_1 = ListNode(3) node2_2 = ListNode(6) node2_1.next = node2_2 node2_3 = ListNode(9) node2_2.next = node2_3 obj = Solution() print(obj.mergeTwoLists(node1_1, node2_1))