def main(): l1 = ListNode(1) l1.next = ListNode(1) l1.next.next = ListNode(3) l2 = ListNode(2) l2.next = ListNode(4) l2.next.next = ListNode(5) inp1 = [l1, l2] print(solution(inp1))
def reverse_list(head): result = None while head is not None: r = ListNode(head.val) r.next = result result = r head = head.next return result
def addAtTail(self, val: int) -> None: n = ListNode(val) if self.tail is None: self.tail = n self.head = n return self.tail.next = n self.tail = n
def main(): # inp1 = sys.stdin.readline().split() # inp2 = sys.stdin.readline().split() inp1 = ListNode(1) inp1.next = ListNode(3) inp2 = ListNode(2) inp2.next = ListNode(5) print(solution(inp1, inp2))
def main(): inp1 = ListNode(1) inp1.next = ListNode(2) inp1.next.next = ListNode(3) inp1.next.next.next = ListNode(4) inp1.next.next.next.next = ListNode(5) print(solution(inp1))
def main(): inp1 = ListNode(0) inp1.next = ListNode(1) inp1.next.next = ListNode(2) inp1.next.next.next = ListNode(3) inp1.next.next.next.next = ListNode(4) inp2 = [0, 3, 1, 4] print(solution(inp1, inp2))
def main(): inp1 = ListNode(4) inp2 = ListNode(5) inp3 = ListNode(2) inp1.next = inp2 inp2.next = inp3 solution(inp2) print(inp1)
def addAtIndex(self, index: int, val: int) -> None: if index == 0: self.addAtHead(val) return h = self.head i = 0 if h is None: return while h.next is not None: if i + 1 == index: n = ListNode(val) n.next = h.next h.next = n return h = h.next i += 1 if i + 1 == index: self.addAtTail(val)
def reverse_list(l1, l2): if l1 is None: return l2 elif l2 is None: return l1 if l1.val > l2.val: result = ListNode(l2.val) l2 = l2.next else: result = ListNode(l1.val) l1 = l1.next r = result while l1 is not None or l2 is not None: if l2 is None: r.next = ListNode(l1.val) l1 = l1.next elif l1 is None: r.next = ListNode(l2.val) l2 = l2.next else: if l1.val > l2.val: r.next = ListNode(l2.val) l2 = l2.next else: r.next = ListNode(l1.val) l1 = l1.next r = r.next return result
def do_reverse(handle: ListNode, k): if handle is None or not big_enough(handle.next, k): return h = handle.next last = handle num = k while num > 0: next_node = h.next h.next = last last = h h = next_node num -= 1 v = handle.next v.next = h handle.next = last do_reverse(v, k)
def do_reverse(handle: ListNode, k): if handle is None or not big_enough(handle.next, k): return h = handle.next # print("started at", handle) last = handle num = k while num > 0: next_node = h.next h.next = last last = h h = next_node num -= 1 v = handle.next v.next = h handle.next = last # print("finished with", handle) do_reverse(v, k)
def mergeKLists(self, lists: list) -> ListNode: result = ListNode(None) all_nodes = [] heap = [] for lst in lists: if lst: all_nodes.append(lst) heapq.heappush(heap, (lst.val, len(all_nodes) - 1)) r = result while heap: current = heapq.heappop(heap) node = all_nodes[current[1]] new = node.next all_nodes.append(new) r.next = node r = r.next if new: heapq.heappush(heap, (new.val, len(all_nodes) - 1)) return result.next
def main(): # inp1 = sys.stdin.readline().split() # inp2 = sys.stdin.readline().split() inp1 = ListNode(4) inp1.next = ListNode(1) print(solution(inp1))
def swap_pairs(head: ListNode) -> ListNode: handle = ListNode(None) handle.next = head do_reverse(handle, 2) return handle.next
def addAtHead(self, val: int) -> None: n = ListNode(val) n.next = self.head self.head = n if self.tail is None: self.tail = n
def reverse_k_group(head: ListNode, k: int) -> ListNode: handle = ListNode(None) handle.next = head do_reverse(handle, k) return handle.next
def main(): inp1 = ListNode(0) inp1.next = ListNode(1) inp1.next.next = ListNode(1) print(solution(inp1))