def add_two_numbers(self, l1, l2): list1 = l1 list2 = l2 list3 = ListNode(0) head = list3 carry_bit = 0 while list1 or list2: val1 = 0 val2 = 0 if list1: val1 = list1.val list1 = list1.next if list2: val2 = list2.val list2 = list2.next val_sum = (val1 + val2 + carry_bit) val = val_sum % 10 list3.next = ListNode(val) list3 = list3.next carry_bit = 1 if val_sum / 10 < 1: carry_bit = 0 if carry_bit: list3.next = ListNode(carry_bit) return head.next
def merge_two_sorted_lists(L1, L2): # TODO - you fill in here. """ Inputs are two head ListNode instances of linked lists of integers; output is a ListNode instance - the head of the combined sorted linked list. Time Complexity: O(m+n) where m is the length of L1 and n is the length of L2 Space Complexity: O(3) which is just O(1) """ new_head = ListNode() p = new_head new_tail = ListNode() new_head.next = new_tail while L1 and L2: if L1.data <= L2.data: new_tail.next = ListNode(L1.data) L1 = L1.next else: new_tail.next = ListNode(L2.data) L2 = L2.next new_tail = new_tail.next if L1: new_tail.next = L1 elif L2: new_tail.next = L2 return new_head.next.next
def mergeTwoLists(self, A, B): sorted = ListNode(-1) head = sorted while not A is None or not B is None: if A is None: if not sorted: return B else: sorted.next = B break if B is None: if not sorted: return A else: sorted.next = A break if A.val < B.val: if sorted is None: sorted = A else: sorted.next = A sorted = sorted.next A = A.next else: if sorted is None: sorted = B else: sorted.next = B sorted = sorted.next B = B.next return head.next
def getSum(self, l1, l2): if self.passToNext == 0: sum = l1.val + l2.val elif self.passToNext == 1: sum = l1.val + l2.val + 1 if sum >= 10: final_result = sum - 10 self.passToNext = 1 else: final_result = sum self.passToNext = 0 result_node = ListNode(final_result) if (l1.next is not None and l2.next is not None): result_node.next = self.getSum(l1.next, l2.next) elif (l1.next is not None and l2.next is None): tempNode = ListNode(0) result_node.next = self.getSum(l1.next, tempNode) elif (l1.next is None and l2.next is not None): tempNode = ListNode(0) result_node.next = self.getSum(tempNode, l2.next) else: if self.passToNext == 1: result_node.next = ListNode(1) else: result_node.next = None return result_node
def merge_two_sorted_lists(L1: Optional[ListNode], L2: Optional[ListNode]) -> Optional[ListNode]: r = ListNode() dummy_head = r while not (L1 == None and L2 == None): if L1 is not None and (L2 is None or L1.data <= L2.data): r.next = L1 L1 = L1.next else: r.next = L2 L2 = L2.next r = r.next return dummy_head.next
def merge_two_sorted_lists(l1, l2): # res = ListNode(0, None) pre = ListNode(0) ans = pre while (l1 != None or l2 != None): if l1 == None or (l2 != None and l2.data <= l1.data): pre.next = l2 l2 = l2.next pre = pre.next else: pre.next = l1 l1 = l1.next pre = pre.next return ans.next
def is_linked_list_a_palindrome(L: ListNode) -> bool: lhalf = ListNode(0) slow = L if not slow: return True fast = slow.next if not fast: return True while fast: temp = slow slow = slow.next lhalf.next, temp.next = temp, lhalf.next if fast.next: fast = fast.next if not fast.next: slow = slow.next fast = fast.next else: fast = fast.next lhalf = lhalf.next while lhalf and slow: if lhalf.data != slow.data: return False lhalf = lhalf.next slow = slow.next return True
def reverse_sublist(L: ListNode, start: int, finish: int) -> Optional[ListNode]: # TODO - you fill in here. res = ListNode() res.next = L count = 1 while count != start: L = L.next count += 1 sub = L L = L.next while count != finish: L.next = sub return None
def reverse_sublist(head: ListNode, s: int, f: int) -> Optional[ListNode]: if not head or s < 1 or f < 1: return head i = 0 p = head prev = None while p and i < s-1: prev = p p = p.next i += 1 q = prev while p and i < f: pnext = p.next p.next = prev prev = p p = pnext i += 1 if q: qnext = q.next q.next = prev qnext.next = p return head else: head.next = p return prev
def deletion_from_list(node_to_delete: ListNode) -> None: if not node_to_delete: return None nachste = node_to_delete.next node_to_delete.data = nachste.data node_to_delete.next = nachste.next
def cyclically_right_shift_list(L, k): if not L: return L dummy = ListNode(0, L) tail = dummy length = 0 while tail.next is not None: length += 1 tail = tail.next k = k % length if k == 0: return L new_tail = dummy for _ in range(length - k): new_tail = new_tail.next tail.next = dummy.next dummy.next = new_tail.next new_tail.next = None return dummy.next
def reverse_sublist(L: ListNode, start: int, finish: int) -> Optional[ListNode]: dummyHead = ListNode() dummyHead.next = L head = dummyHead tail = L count = 1 # get sublist bounds. Head points right before the first element in sublist # and tail should point after last element in sublist while tail and count <= finish : if count < start : head = head.next tail = tail.next count +=1 curr = head.next end = tail while curr != tail : tmp = curr curr = curr.next tmp.next = end end = tmp head.next = end return dummyHead.next
def main(): solver = Solution() head = ListNode(3) head.next = ListNode(2) solver.sortList2(head) ptr = head while ptr: print(ptr.val) ptr = ptr.next head = ListNode(2) head.next = ListNode(1) solver.sortList2(head) ptr = head while ptr: print(ptr.val) ptr = ptr.next
def remove_kth_last(head, n): if head.next is None: return None count = 0 pre = ListNode(0) pre.next = head while (pre.next is not None): pre = pre.next count += 1 pre2 = head ans = pre2 if n == 1: while (count != 2): count -= 1 pre2 = pre2.next pre2.next = None else: while (count != n): count -= 1 pre2 = pre2.next pre2.data = pre2.next.data # must data first, then next pre2.next = pre2.next.next return ans
def test_node_delete(self): # Failure message: # Your `Node.delete` method did not work as expected node_1 = ListNode(3) node_2 = ListNode(4) node_3 = ListNode(5) node_1.next = node_2 node_2.next = node_3 node_2.prev = node_1 node_3.prev = node_2 node_2.delete() self.assertEqual(node_1.next, node_3) self.assertEqual(node_3.prev, node_1)
def merge_two_sorted_lists(L1: Optional[ListNode], L2: Optional[ListNode]) -> Optional[ListNode]: head = ListNode() cpy = head while L1 and L2: if L1.data < L2.data: head.next = L1 L1 = L1.next else: head.next = L2 L2 = L2.next head = head.next head.next = L1 or L2 return cpy.next
def test_case_with_one_more_digit_sum_result(self): addTwoNumbersInstance = addTwoNumbers() l1_3 = ListNode(6) l1_2 = ListNode(5) l1_2.next = l1_3 l1_1 = ListNode(9) l1_1.next = l1_2 l2_3 = ListNode(8) l2_2 = ListNode(4) l2_2.next = l2_3 l2_1 = ListNode(9) l2_1.next = l2_2 result_4 = ListNode(1) result_3 = ListNode(5) result_3.next = result_4 result_2 = ListNode(0) result_2.next = result_3 result_1 = ListNode(8) result_1.next = result_2 expected_result = [ result_1.val, result_2.val, result_3.val, result_4.val ] returned_result = addTwoNumbersInstance.addTwoNumbersSolution( l1_1, l2_1) returned_result_list = addTwoNumbersInstance.toList( returned_result, []) print(returned_result_list) self.assertEqual(returned_result_list, expected_result)
def even_odd_merge(L: ListNode) -> Optional[ListNode]: if L is None: return None odd, even = ListNode(0), ListNode(0) head_even, head_odd = even, odd index = 0 while L: if index % 2 == 0: even.next = L even = even.next else: odd.next = L odd = odd.next L = L.next index += 1 odd.next = None even.next = head_odd.next return head_even.next
def reverse_list_1(L: ListNode) -> ListNode: if not L or not L.next: return L new_head = reverse_list_1(L.next) L.next.next = L L.next = None return new_head
def test_case_with_different_length_short_test(self): addTwoNumbersInstance = addTwoNumbers() l1_2 = ListNode(8) l1_1 = ListNode(1) l1_1.next = l1_2 l2_1 = ListNode(0) result_2 = ListNode(8) result_1 = ListNode(1) result_1.next = result_2 expected_result = [result_1.val, result_2.val] returned_result = addTwoNumbersInstance.addTwoNumbersSolution( l1_1, l2_1) returned_result_list = addTwoNumbersInstance.toList( returned_result, []) print(returned_result_list) self.assertEqual(returned_result_list, expected_result)
def merge_list(head_a, head_b): pa = head_a pb = head_b pre = ListNode(-1) new_head = pre while pa and pb: if pa.val > pb.val: pre.next = pb pb = pb.next else: pre.next = pa pa = pa.next pre = pre.next if pa: pre.next = pa if pb: pre.next = pb return new_head.next
def reverse_list(head: ListNode) -> ListNode: dummy = ListNode(0) while head: dummy.next, head.next, head = head, dummy.next, head.next #tmp1 = dummy.next #tmp2 = head.next #dummy.next = head #head.next = tmp1 #head = tmp2 return dummy.next
def list_pivoting(l: ListNode, x: int) -> Optional[ListNode]: less_than, equal_to, greater_than = ListNode(0), ListNode(0), ListNode(0) dummy_less, dummy_equal, dummy_greater = less_than, equal_to, greater_than while l: if l.data < x: less_than.next = l less_than = less_than.next elif l.data == x: equal_to.next = l equal_to = equal_to.next else: greater_than.next = l greater_than = greater_than.next l = l.next greater_than.next = None equal_to.next = dummy_greater.next less_than.next = dummy_equal.next return dummy_less.next
def pair(list1, list2): # no tail pointer but done recursivly so no O(n^2) if not list1 or not list2: # if either list is over, finish our list return None # wtih a None and end recursion else: this_node = ListNode((list1.val, list2.val)) # gets the next node by calling this function with the next item # in each list next_node = pair(list1.next, list2.next) this_node.next = next_node return this_node
def pair_recursive(head1, head2): #if either list is empty, we have reached the end and finish the list with None if head1 is None or head2 is None: return None else: # makes a list node and sets its value to a tup of the values of each lists heads node = ListNode((head1.val, head2.val)) # sets its next node to the returned node of this function on the two lists # without thier heads node.next = pair_recursive(head1.next, head2.next) return node
def add_two_numbers(L1, L2): L = ListNode() dummy_head = L carry = 0 while L1 and L2: summ = L1.data + L2.data + carry if summ >= 10: L.next = ListNode(summ % 10) carry = 1 else: L.next = ListNode(summ) carry = 0 L = L.next L1, L2 = L1.next, L2.next while L1: if carry == 0: L.next = L1 break summ = L1.data + carry if summ >= 10: L.next = ListNode(summ % 10) carry = 1 else: L.next = ListNode(summ) carry = 0 L, L1 = L.next, L1.next while L2: if carry == 0: L.next = L2 break summ = L2.data + carry if summ >= 10: L.next = ListNode(summ % 10) carry = 1 else: L.next = ListNode(summ) carry = 0 L, L2 = L.next, L2.next if carry != 0: L.next = ListNode(carry) return dummy_head.next
def array_to_list(input_array): '''this fuction works recursivly by creating a node with the first value in the list, then setting its next node to the return value of this fuction called again minus its first value. once there are no more values in the array, it returns None, finishing off the linked list and ending the recursion''' if input_array: node = ListNode(input_array[0]) node.next = array_to_list(input_array[1:]) return node else: return None
def array_to_list_recursive(data): # if there are any elements in the array if data: # created a list node with the current first value then sets its next value to # the node returned by this function called on the list without the first index node = ListNode(data[0]) child_node = array_to_list_recursive(data[1:]) node.next = child_node return node else: # once there are no more items in the array, finish off the list with None return None
def test_case_sample_value(self): addTwoNumbersInstance = addTwoNumbers() l1_3 = ListNode(3) l1_2 = ListNode(4) l1_2.next = l1_3 l1_1 = ListNode(2) l1_1.next = l1_2 l2_3 = ListNode(4) l2_2 = ListNode(6) l2_2.next = l2_3 l2_1 = ListNode(5) l2_1.next = l2_2 result_3 = ListNode(8) result_2 = ListNode(0) result_2.next = result_3 result_1 = ListNode(7) result_1.next = result_2 expected_result = [result_1.val, result_2.val, result_3.val] returned_result = addTwoNumbersInstance.addTwoNumbersSolution( l1_1, l2_1) # result = [returned_result.val] # result.append(returned_result.next.val) # result.append(returned_result.next.next.val) returned_result_list = addTwoNumbersInstance.toList( returned_result, []) print(returned_result_list) self.assertEqual(returned_result_list, expected_result)
def merge_two_sorted_lists(L1: Optional[ListNode], L2: Optional[ListNode]) -> Optional[ListNode]: dummy = ListNode(0) dummyHead = dummy L1Dummy = L1 L2Dummy = L2 while L1Dummy is not None or L2Dummy is not None: if L1Dummy is None: dummy.next = L2Dummy L2Dummy = L2Dummy.next elif L2Dummy is None: dummy.next = L1Dummy L1Dummy = L1Dummy.next else: if L1Dummy.data < L2Dummy.data: dummy.next = L1Dummy L1Dummy = L1Dummy.next else: dummy.next = L2Dummy L2Dummy = L2Dummy.next dummy = dummy.next return dummyHead.next
def add_front(self, item): # Check the item if it is Listnode. if not isinstance(item, ListNode): item = ListNode(item) # First item. if self.head == None: self.head = item self.tail = item # Add the item front of the link list. else: item.next = self.head self.head = item
def sortList2(self, head): if not head or not head.next: return head n = 0 ptr = head while ptr: ptr = ptr.next n += 1 step = 1 sentry = ListNode(0) while step < n: sentry.next = head prev = sentry while prev.next: head1 = prev.next tail1 = head1 for i in range(step - 1): if tail1.next is None: break tail1 = tail1.next if tail1.next is None: break head2 = tail1.next tail2 = head2 tail1.next = None for i in range(step - 1): if tail2.next is None: break tail2 = tail2.next post = tail2.next tail2.next = None prev.next = self.merge(head1, head2) tail = prev.next while tail.next: tail = tail.next tail.next = post prev = tail head = sentry.next step <<= 1 return head
if A == None: return B elif B == None: return A if A.val < B.val: A.next = self.mergeTwoLists(A.next, B) return A else: B.next = self.mergeTwoLists(A, B.next) return B s = Solution() a1 = ListNode(1) a2 = ListNode(5) a3 = ListNode(9) a1.next = a2 a2.next = a3 b1 = ListNode(2) b2 = ListNode(3) b3 = ListNode(10) b1.next = b2 b2.next = b3 merged = s.mergeTwoLists(a1, b1) tmp = merged while tmp.next != None: print(tmp.val) tmp = tmp.next print(tmp.val)