def swapPairs(self, head: ListNode) -> ListNode: if head is not None: pre = head curr = head next = curr.next while curr is not None and curr.next is not None: # swap node curr.next = next.next next.next = curr if curr == head: pre = next head = pre else: pre.next = next pre = pre.next # move to next node pre = curr curr = curr.next if curr is not None: next = curr.next head.print_all() return head
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: curr1 = l1 curr2 = l2 carry = 0 total = None current = None while curr1 != None or curr2 != None: sum = 0 sum += carry if carry != 0 else 0 sum += curr1.val if curr1 != None else 0 sum += curr2.val if curr2 != None else 0 if sum >= 10: sum -= 10 carry = 1 else: carry = 0 if total == None: total = ListNode(sum) current = total else: current.next = ListNode(sum) current = current.next curr1 = curr1.next if curr1 != None else None curr2 = curr2.next if curr2 != None else None if carry > 0: current.next = ListNode(carry) return total
def list_node_generator(nums): if len(nums) == 0: return None t = ListNode(nums[0]) head = t for i in range(1, len(nums)): t.next = ListNode(nums[i]) t = t.next return head
def array_to_list_node(data: []): if data is None or len(data) == 0: return None node = ListNode(data[0]) curr = node for i in range(1, len(data)): curr.next = ListNode(data[i]) curr = curr.next return node
def method1(ln): if ln is None or ln.next is None: return ln arr = [] while ln.next: arr.append(ln.val) ln = ln.next arr.append(ln.val) ll = res = ListNode(0) for i in arr[::-1]: res.next = ListNode(i) res = res.next return ll.next
def add_two_numbers3(l1, l2): l3 = ListNode(0) res = l3 carry = 0 while l1 or l2 or carry: l3.next = ListNode(0) if l1: carry += l1.val l1 = l1.next if l2: carry += l2.val l2 = l2.next # 获得商和余数 carry, val = divmod(carry, 10) l3.next.val = val l3 = l3.next return res.next
def method3(l): if l is None: return l i = ListNode(l.val) j = l.next while j: temp = j.next j.next = i i = j j = temp return i
def merge_two_lists(l1, l2): t = res = ListNode(0) while l1 and l2: if l1.val <= l2.val: res.next = l1 l1 = l1.next else: res.next = l2 l2 = l2.next res = res.next res.next = l1 or l2 return t.next
def merge_k_lists2(lists): res = temp = ListNode(0) q = PriorityQueue() for list_node in lists: if list_node: q.put((list_node.val, list_node)) while not q.empty(): # PriorityQueue :每次get取最小的值 temp.next = q.get()[1] temp = temp.next if temp.next: q.put((temp.next.val, temp.next)) return res.next
def reverse_k_group(head, k): t = head n = 0 res = temp = ListNode(0) while t: n += 1 if n == k: tt = t.next t.next = None temp.next = reversed(head) while temp.next: temp = temp.next temp.next = reverse_k_group(tt, k) return res.next else: t = t.next if head: temp.next = head return res.next
def Merge(self, pHead1, pHead2): # write code here res = tmp = ListNode(0) while pHead1 and pHead2: if pHead1.val <= pHead2.val: tmp.next = pHead1 pHead1 = pHead1.next else: tmp.next = pHead2 pHead2 = pHead2.next tmp = tmp.next while pHead1: tmp.next = pHead1 pHead1 = pHead1.next tmp = tmp.next while pHead2: tmp.next = pHead2 pHead2 = pHead2.next tmp = tmp.next return res.next
def add_two_numbers2(l1, l2): l3 = ListNode(0) res = l3 carry = 0 while l1 or l2: l3.next = ListNode(0) if l1: carry += l1.val l1 = l1.next if l2: carry += l2.val l2 = l2.next l3.next.val = carry % 10 carry = carry // 10 l3 = l3.next if carry > 0: l3.next = ListNode(0) l3.next.val = carry return res.next
def insert_at_end(self, data): new_node = ListNode(data) curr = self.head while curr.next is not None: curr = curr.next curr.next = new_node
def insert_at_beginning(self, data): new_node = ListNode(data) new_node.next = self.head self.head = new_node
def add_two_numbers1(l1, l2): l3 = ListNode(0) res = l3 temp = 0 while l1 and l2: l3.val = (l1.val + l2.val + temp) % 10 temp = (l1.val + l2.val + temp) // 10 l1 = l1.next l2 = l2.next if l1 or l2: l3.next = ListNode(0) l3 = l3.next while l1: l3.val = (l1.val + temp) % 10 temp = (l1.val + temp) // 10 l1 = l1.next if l1: l3.next = ListNode(0) l3 = l3.next while l2: l3.val = (l2.val + temp) % 10 temp = (l2.val + temp) // 10 l2 = l2.next if l2: l3.next = ListNode(0) l3 = l3.next if temp > 0: l3.next = ListNode(0) l3.next.val = temp return res
sum += carry if carry != 0 else 0 sum += curr1.val if curr1 != None else 0 sum += curr2.val if curr2 != None else 0 if sum >= 10: sum -= 10 carry = 1 else: carry = 0 if total == None: total = ListNode(sum) current = total else: current.next = ListNode(sum) current = current.next curr1 = curr1.next if curr1 != None else None curr2 = curr2.next if curr2 != None else None if carry > 0: current.next = ListNode(carry) return total l1 = ListNode(1) l2 = ListNode(9) l2.next = ListNode(9) s = Solution() total = s.addTwoNumbers(l1, l2)
def test_add_two_numbers(): ''' 思路: 时间复杂度:O(n) ''' def add_two_numbers1(l1, l2): l3 = ListNode(0) res = l3 temp = 0 while l1 and l2: l3.val = (l1.val + l2.val + temp) % 10 temp = (l1.val + l2.val + temp) // 10 l1 = l1.next l2 = l2.next if l1 or l2: l3.next = ListNode(0) l3 = l3.next while l1: l3.val = (l1.val + temp) % 10 temp = (l1.val + temp) // 10 l1 = l1.next if l1: l3.next = ListNode(0) l3 = l3.next while l2: l3.val = (l2.val + temp) % 10 temp = (l2.val + temp) // 10 l2 = l2.next if l2: l3.next = ListNode(0) l3 = l3.next if temp > 0: l3.next = ListNode(0) l3.next.val = temp return res # 方法一优化 def add_two_numbers2(l1, l2): l3 = ListNode(0) res = l3 carry = 0 while l1 or l2: l3.next = ListNode(0) if l1: carry += l1.val l1 = l1.next if l2: carry += l2.val l2 = l2.next l3.next.val = carry % 10 carry = carry // 10 l3 = l3.next if carry > 0: l3.next = ListNode(0) l3.next.val = carry return res.next # 方法2 优化,pythonic def add_two_numbers3(l1, l2): l3 = ListNode(0) res = l3 carry = 0 while l1 or l2 or carry: l3.next = ListNode(0) if l1: carry += l1.val l1 = l1.next if l2: carry += l2.val l2 = l2.next # 获得商和余数 carry, val = divmod(carry, 10) l3.next.val = val l3 = l3.next return res.next print() t1 = ListNode(0) t2 = ListNode(0) test1 = t1 test2 = t2 for i in range(1): t1.val = i + 1 t1.next = ListNode(2) t1 = t1.next for i in range(1): t2.val = 9 t2.next = ListNode(9) t2 = t2.next print(test1) print(test2) print(add_two_numbers3(test1, test2))