def addTwoNumbers_work(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ dummyHead = ListNode(0) p, q, curr, carry = l1, l2, dummyHead, 0 while p is not None or q is not None: if p is not None: x = p.val else: x = 0 if q is not None: y = q.val else: y = 0 sum = carry + x + y carry = sum // 10 curr.next = ListNode(sum % 10) curr = curr.next if p is not None: p = p.next if q is not None: q = q.next if carry > 0: curr.next = ListNode(carry) return dummyHead.next
def oddEvenList2(self, head): # 48ms if head is None: return None count = 0 list_odd, list_even = [], [] while head is not None: if count % 2 == 0: list_even.append(head.val) else: list_odd.append(head.val) count += 1 head = head.next if len(list_even) > 0: res = ListNode(list_even[0]) res_head = res for i in range(1, len(list_even)): res.next = ListNode(list_even[i]) res = res.next if len(list_odd) > 0: res.next = ListNode(list_odd[0]) res = res.next for i in range(1, len(list_odd)): res.next = ListNode(list_odd[i]) res = res.next return res_head
def addTwoNumbers(self, l1, l2): carry = 0 root = n = ListNode(0) while l1 or l2 or carry: v1 = v2 = 0 if l1: v1 = l1.val l1 = l1.next if l2: v2 = l2.val l2 = l2.next carry, val = divmod(v1+v2+carry, 10) n.next = ListNode(val) n = n.next return root.next
def insertionSortList(self, head: ListNode) -> ListNode: # 40ms if not head: return head nodeList = [] cur = head while cur != None: nodeList.append(cur.val) cur = cur.next nodeList.sort() print(nodeList) res = ListNode(nodeList[0]) cur = res for i in range(1, len(nodeList)): cur.next = ListNode(nodeList[i]) cur = cur.next return res
def merge(self, h1, h2): dummy = tail = ListNode(None) while h1 and h2: if h1.val < h2.val: tail.next, tail, h1 = h1, h1, h1.next else: tail.next, tail, h2 = h2, h2, h2.next tail.next = h1 or h2 return dummy.next
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: # 64ms l1_str, l2_str = "", "" temp = l1 while temp is not None: l1_str += str(temp.val) temp = temp.next temp = l2 while temp is not None: l2_str += str(temp.val) temp = temp.next res_str = str(int(l1_str) + int(l2_str)) res = ListNode(res_str[0]) i = 1 temp = res while i < len(res_str): temp.next = ListNode(res_str[i]) temp = temp.next i += 1 return res
def removeElements(self, head, val): dummy = ListNode(-1) curr = dummy while head: if head.val != val: curr.next = head curr = curr.next head = head.next else: head = head.next curr.next = None return dummy.next
def partition(self, head, x): # 32ms cur = head smaller_sentinel = ListNode(None) smaller_cur = smaller_sentinel larger_sentinel = ListNode(None) larger_cur = larger_sentinel while cur is not None: if cur.val < x: smaller_cur.next = cur smaller_cur = smaller_cur.next else: larger_cur.next = cur larger_cur = larger_cur.next cur = cur.next larger_cur.next = None smaller_cur.next = larger_sentinel.next return smaller_sentinel.next
def mergeKLists_work(self, lists: 'List[ListNode]') -> 'ListNode': if len(lists) < 1: return None if lists is None: return None none_count = 0 for temp_list in lists: if temp_list is None: none_count += 1 if none_count == len(lists): return None current_lists = lists work_list = None while True: current_min = sys.maxsize end_count = 0 for i in range(len(current_lists)): if current_lists[i] is None: end_count += 1 continue if current_lists[i].val < current_min: current_index = i current_min = current_lists[i].val if end_count == len(current_lists): break if work_list is None: root = ListNode(current_min) work_list = root else: work_list.next = ListNode(current_min) work_list = work_list.next current_lists[current_index] = current_lists[current_index].next return root
def reverseList(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None: return None node_list = [] temp_node = head node_list.append(head.val) while temp_node.next is not None: temp_node = temp_node.next node_list.append(temp_node.val) result_top = ListNode(node_list[len(node_list) - 1]) temp_node = result_top for i in range(len(node_list) - 2, -1, -1): temp_node.next = ListNode(node_list[i]) temp_node = temp_node.next return result_top
def rotateRight(self, head, k): # 24ms if head is None or head.next is None: return head dummy = ListNode(0) dummy.next = head fast, slow = dummy, dummy i = 0 while fast.next is not None: fast = fast.next i += 1 j = i - k % i while j > 0: slow = slow.next j -= 1 fast.next = dummy.next dummy.next = slow.next slow.next = None return dummy.next
def insertionSortList2(self, head: ListNode) -> ListNode: # 128ms p = dummy = ListNode(0) cur = dummy.next = head while cur and cur.next: val = cur.next.val if cur.val < val: cur = cur.next continue if p.next.val > val: p = dummy while p.next.val < val: p = p.next temp = cur.next cur.next = temp.next temp.next = p.next p.next = temp return dummy.next
def reorderList(self, head): """ :type head: ListNode :rtype: void Do not return anything, modify head in-place instead. """ t = head a = [] while t: a.append(t) t = t.next if len(a) < 3: return # print(a[1].next.val) for i in range(0, len(a) // 2): tmp = a[i].next a[i].next = a[~i] if tmp == a[~i]: a[~i].next = None else: a[~i].next = tmp tmp.next = None test = False if test: from ListNode.ListNode import ListNode ll = ListNode.make([1, 2, 3, 4, 5]) s = Solution() print(s.reorderList(ll)) print(ll)
class Solution: def insertionSortList(self, head): """ :type head: ListNode :rtype: ListNode """ if head is None: return head out = [] while head: out.append(head) head = head.next # print(out[-1].val) out = sorted(out, key=lambda x: x.val) # print([x.val for x in out]) for i in range(0, len(out) - 1): out[i].next = out[i + 1] out[len(out) - 1].next = None return out[0] test = False if test: from ListNode.ListNode import ListNode ll = ListNode.make([5, 1, 2, 3, 4]) s = Solution() print(s.insertionSortList(ll))