def merge_two_sorted_lists(L1, L2): # head of both lists curr_1 = L1.head curr_2 = L2.head # empty linked list new_ll = LinkedList() # traverse while still nodes and compare which the least to new_ll while curr_1 and curr_2: if curr_1.data <= curr_2.data: new_ll.addNode(Node(curr_1.data)) curr_1 = curr_1.next else: new_ll.addNode(Node(curr_2.data)) curr_2 = curr_2.next # appends remaining nodes of L1 or L2 if not curr_1: while curr_2: new_ll.addNode(Node(curr_2.data)) curr_2 = curr_2.next elif not curr_2: while curr_1: new_ll.addNode(Node(curr_1.data)) curr_1 = curr_1.next # return the new list return new_ll
def add_two_numbers(L1: LinkedList, L2: LinkedList): final_ll = LinkedList() L1_curr = L1.head L2_curr = L2.head carry = 0 while L1_curr or L2_curr or carry: val = carry + (L1_curr.data if L1_curr else 0) + (L2_curr.data if L2_curr else 0) L1_curr = L1_curr.next if L1_curr else None L2_curr = L2_curr.next if L2_curr else None final_ll.addNode(Node(val % 10)) carry = val // 10 return final_ll
# naive: append everything and sort in place # time: O(n+m) # space: O(1) l1 = LinkedList() l2 = LinkedList() node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node5 = Node(5) node6 = Node(6) node7 = Node(7) l1.addNode(node1) l2.addNode(node2) l1.addNode(node3) l2.addNode(node4) l1.addNode(node5) l2.addNode(node6) l1.addNode(node7) print("\n") l1.print_list() print("\n") l2.print_list() print("\n") x = merge_two_sorted_lists(l1, l2)
# set temp next to current # and previous next should be temp else: temp.set_next(current) previous.set_next(temp) return L1 if __name__ == "__main__": # two pointers previous and current with a special condition for empty linkedlist # time: O(n) # space: O(1) l1 = LinkedList() node1 = Node(1) node2 = Node(2) node4 = Node(4) node5 = Node(5) l1.addNode(node1) l1.addNode(node2) l1.addNode(node4) l1.addNode(node5) print("\n") x = orderedInsert(l1, 3) x.print_list()
return L1 if __name__ == "__main__": # naive: traverse two times, first to record length of list and to subtract k from it to see which element to stop at # the first is k up ahead, by the time it stops the second will be at kth last # time: O(n) # space: O(1) l1 = LinkedList() node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node5 = Node(5) node6 = Node(6) node7 = Node(7) l1.addNode(node1) l1.addNode(node2) l1.addNode(node3) l1.addNode(node4) l1.addNode(node5) l1.addNode(node6) l1.addNode(node7) x = remove_kth_last(l1, 2) print("\n") x.print_list()
if __name__ == "__main__": # even list before odd list # first is index 0 # time: O(n) # space: O(1) l1 = LinkedList() node0 = Node(0) node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node5 = Node(5) node6 = Node(6) node7 = Node(7) l1.addNode(node0) l1.addNode(node1) l1.addNode(node2) l1.addNode(node3) l1.addNode(node4) l1.addNode(node5) l1.addNode(node6) l1.addNode(node7) print("\n") x = even_odd_merge(l1) x.print_list()
# naive: # grade school algorithm # time: O(max(n,m)) - n,m length of two lists # space: O(1) l1 = LinkedList() l2 = LinkedList() node1 = Node(3) node2 = Node(1) node3 = Node(4) node4 = Node(7) node5 = Node(0) node6 = Node(9) l1.addNode(node1) l1.addNode(node2) l1.addNode(node3) l2.addNode(node4) l2.addNode(node5) l2.addNode(node6) print("\n") l1.print_list() print("\n") l2.print_list() print("\n") x = add_two_numbers(l1, l2) x.print_list()
if __name__ == "__main__": # naive: maintain 3 lists and create new nodes for each. # instead attach existing nodes to chain # time: O(n) # space: O(1) ll = LinkedList() ll.print_list() node1 = Node(3) node2 = Node(2) node3 = Node(2) node4 = Node(11) node5 = Node(7) node6 = Node(5) node7 = Node(11) ll.addNode(node1) ll.addNode(node2) ll.addNode(node3) ll.addNode(node4) ll.addNode(node5) ll.addNode(node6) ll.addNode(node7) ll.print_list() print("\n") x=list_pivoting(ll,7) x.print_list()
last = current current = nextNode # set the head to the very last node L1.head = last return L1 if __name__ == "__main__": # uses three pointers one for current, last, and nextnode # time: O(n) # space: O(1) linkedlst = LinkedList() linkedlst.addNode(Node(1)) linkedlst.addNode(Node(2)) linkedlst.addNode(Node(3)) linkedlst.addNode(Node(4)) x = reverseList(linkedlst) # last=None,curr=1->2->3->4 # next=2->3->4, curr=1,last=1,curr=2->3->4 # next=3->4,curr=2->1,last=2->1,curr=3->4 # next=4,curr=3->2->1,last=3->2->1,curr=4 # next=None,curr=4->3->2->1,last=4->3->2-1,curr=None x.print_list()
# time: O(n) # space: O(1) l1 = LinkedList() l2 = LinkedList() node0 = Node(0) node1 = Node(1) node2 = Node(2) node3 = Node(3) node4 = Node(4) node5 = Node(5) node6 = Node(6) node7 = Node(7) l1.addNode(node0) l1.addNode(node1) l1.addNode(node2) l1.addNode(node3) l1.addNode(node4) l1.addNode(node5) print("\n") l1.print_list() print(l1.length()) l2.addNode(node6) l2.addNode(node7) l2.addNode(node3) l2.addNode(node4) l2.addNode(node5)