def remove_nth_frm_end(head, n): """ 2 pointer algo :type head: ListNode :type n: int :rtype: ListNode """ tmp = Node(-1) tmp.next = head pointer_1 = tmp pointer_2 = tmp # Move over n nodes including dummy for i in range(n): pointer_2 = pointer_2.next # set pointer_2 to n-1 th # Increment ptrs till you reach end of list while pointer_2.next: pointer_1 = pointer_1.next pointer_2 = pointer_2.next # Now pointer_1.next points to the nth node from the end pointer_1.next = pointer_1.next.next return head
def merge_k_lists(lists): """ :type lists: List[ListNode] :rtype: ListNode """ head = point = Node(0) q = PriorityQueue() for l in lists: q.put((l.val, id(l), l)) while not q.empty(): # get the lowest from queue val, _id, node = q.get() point.next = Node(val) point = point.next node = node.next if node: q.put((node.val, id(node), node)) return head.next
def add_two_numbers(l1, l2, carry=0): """ :type l1: Node :type l2: Node :rtype: Node """ val = l1.val + l2.val + carry carry = val // 10 final_ll = Node(val % 10) if (l1.next != None or l2.next != None or carry != 0): if l1.next == None: l1.next = Node(0) if l2.next == None: l2.next = Node(0) final_ll.next = add_two_numbers(l1.next, l2.next, carry) return final_ll
def swap_pairs(head): """ :type head: Node :rtype: Node """ t = prev = Node(0) prev.next = head # 0 1 2 3 4 while head and head.next: prev.next = head.next # 0 2 3 4 head.next = head.next.next # 1 3 4 prev.next.next = head # 0 2 1 3 4 head = head.next # 3 4 prev = prev.next.next # 1 3 4 return t.next
def merge_two_lists_sol2(l1, l2): dummy = cur = Node(0) while l1 and l2: if l1.val < l2.val: # l2's value is less than l1's so ke # add this value to curr as next element cur.next = l1 l1 = l1.next else: # l1's value is less than l2's # add this value to curr as next element cur.next = l2 l2 = l2.next cur = cur.next cur.next = l1 or l2 return dummy.next
# Check for edge case of not having enough nodes! if not right_pointer.nex: raise LookupError('Error: n is larger than the linked list.') # Otherwise, we can set the block right_pointer = right_pointer.nex # Move the block down the linked list while right_pointer.nex: left_pointer = left_pointer.nex right_pointer = right_pointer.nex # Now return left pointer, its at the nth to last element! return left_pointer.value a = Node(1) b = Node(2) c = Node(3) d = Node(4) e = Node(5) # 1 2 3 4 5 a.nex = b b.nex = c c.nex = d d.nex = e print nth_to_last_node_sol1(4, a) print nth_to_last_node_sol2(4, a)
val = l1.val + l2.val + carry carry = val // 10 final_ll = Node(val % 10) if (l1.next != None or l2.next != None or carry != 0): if l1.next == None: l1.next = Node(0) if l2.next == None: l2.next = Node(0) final_ll.next = add_two_numbers(l1.next, l2.next, carry) return final_ll l1 = Node(2) node2 = Node(4) node3 = Node(3) l1.next = node2 node2.next = node3 l2 = Node(5) node2 = Node(6) node3 = Node(4) l2.next = node2 node2.next = node3 fl = add_two_numbers(l1, l2)
next = None # until we have gone through to the end of the list while current: # Make sure to copy the current nodes next node to a variable next # Before overwriting as the previous node for reversal next = current.next # Reverse the pointer of the next current.next = previous # Go one forward in the list previous = current current = next return previous # Create a list of 4 nodes a = Node(1) b = Node(2) c = Node(3) d = Node(4) # Set up order a,b,c,d with values 1,2,3,4 a.next = b b.next = c c.next = d reverse(a) print(traverse_list(a))