def mergeKLists(self, lists: List[ListNode]) -> ListNode: if not lists: return min_heap = PriorityQueue() # load node values of all lists into minheap for list_item in lists: cur_node = list_item while cur_node: min_heap.put(cur_node.val) cur_node = cur_node.next res_header: ListNode = ListNode(-1) cur_res = res_header while not min_heap.empty(): val = min_heap.get() node: ListNode = ListNode(val) cur_res.next = node cur_res = node return res_header.next
def addLists(self, l1, l2): # write your code here c = 0 dummy = ListNode(0) cur = dummy while l1 or l2: val1 = l1.val if l1 else 0 val2 = l2.val if l2 else 0 val = val1 + val2 + c if val >= 10: val -= 10 c = 1 else: c = 0 node = ListNode(val) cur.next = node cur = cur.next if l1: l1 = l1.next if l2: l2 = l2.next if c: node = ListNode(c) cur.next = node return dummy.next
def addTwoNumbers(l1, l2): """ You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. @l1, listNode @l2, listNode rtype: listNode """ if l1 is None: return l2 elif l2 is None: return l1 else: ret = ListNode(0) cur = ret # python class like pointer carry = 0 # deal with sum more than 10 while l1 or l2: sum2 = 0 if l1: sum2 += l1.val l1 = l1.next if l2: sum2 += l2.val l2 = l2.next sum2 += carry cur.next = ListNode(sum2 % 10) cur = cur.next carry = 1 if sum2 >= 10 else 0 if carry: cur.next = ListNode(1) ret = ret.next # pop the first node that not use return ret
def rearrange_linked_list(self, head: ListNode) -> ListNode: # find middle of the linked list slow, fast = head, head head_backup = head prev = slow while fast and fast.next: prev = slow slow = slow.next fast = fast.next.next #print(slow.val) # reverse second half of the linked list rev_head = self.reverse_linked_list(slow) #print(rev_head.val) prev.next = None # rearrange the linked list while head and rev_head: head_next = head.next rev_head_next = rev_head.next head.next = rev_head rev_head.next = head_next head = head_next rev_head = rev_head_next # return head of linked list return head_backup
def swapPairs(self, head): # write your code here dummy = ListNode(0, head) cur = dummy while cur and cur.next and cur.next.next: tmp = cur.next cur.next = cur.next.next tmp.next = cur.next.next cur.next.next = tmp cur = cur.next.next return dummy.next
def reverse_linked_list(self, head: ListNode) -> ListNode: prev = None while head: _next = head.next head.next = prev prev = head head = _next return prev
def partition(self, head, x): # write your code here dummy = ListNode(0) dummy.next = head pre = insert_node = dummy while head: if head.val < x: if insert_node.next != head: pre.next = head.next node = insert_node.next insert_node.next = head head.next = node head = pre.next insert_node = insert_node.next else: insert_node = head pre = head head = head.next else: pre = head head = head.next return dummy.next
def mergeKLists(self, lists): # write your code here heap = [] sequence = 1 for node in lists: while node: heapq.heappush(heap, (node.val, sequence, node)) sequence += 1 node = node.next if not heap: return None pre = dummy = ListNode(-1) while heap: val, sequence, node = heapq.heappop(heap) pre.next = node pre = node return dummy.next
def merge(self, left, right): dummy = ListNode(0) cur = dummy while left and right: if left.val < right.val: cur.next = left left = left.next else: cur.next = right right = right.next cur = cur.next while left: cur.next = left left = left.next cur = cur.next while right: cur.next = right right = right.next cur = cur.next return dummy.next
def mergeTwoLists(self, l1, l2): # write your code here dummy = ListNode(0) cur = dummy while l1 and l2: if l1.val < l2.val: cur.next = l1 l1 = l1.next else: cur.next = l2 l2 = l2.next cur = cur.next while l1: cur.next = l1 l1 = l1.next cur = cur.next while l2: cur.next = l2 l2 = l2.next cur = cur.next return dummy.next
def reorderList(self, head): # write your code here if not head or not head.next: return head slow, fast = head, head.next while fast and fast.next: slow = slow.next fast = fast.next.next right = self.reverseList(slow.next) slow.next = None dummy = ListNode(0, head) while right: tmp1 = head.next tmp2 = right.next head.next = right right.next = tmp1 head = tmp1 right = tmp2 return dummy.next
def binaryTreeToLists(self, root): # Write your code here res = [] if not root: return res q = deque([root]) while q: head, previous = None, None for _ in range(len(q)): treeNode = q.popleft() listNode = ListNode(treeNode.val) if not head: head = listNode if previous: previous.next = listNode previous = listNode if treeNode.left: q.append(treeNode.left) if treeNode.right: q.append(treeNode.right) res.append(head) return res
def mergeKLists(self, lists): # write your code here heap = [] sequence = 1 for node in lists: while node: heapq.heappush(heap, (node.val, sequence, node)) sequence += 1 node = node.next if not heap: return None pre = dummy = ListNode(-1) while heap: val, sequence, node = heapq.heappop(heap) pre.next = node pre = node return dummy.next s = Solution() node = ListNode(1) head = s.mergeKLists([node]) printList(head) node4 = ListNode(4) node2 = ListNode(2, node4) node1 = ListNode(-1) head = s.mergeKLists([node2, None, node1]) printList(head)