def orderedInsert(L1, item): current = L1.head previous = None stop = False # traverse until you reach where data > item # at this point stop is True and youll have the current and previous pointers while current != None and not stop: if current.get_data() > item: stop = True else: previous = current current = current.get_next() temp = Node(item) # if empty linked list if previous == None: L1.head = temp # if not empty linked list # set temp next to current # and previous next should be temp else: temp.set_next(current) previous.set_next(temp) return L1
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 even_odd_merge(L: LinkedList): curr = L.head final_ll = LinkedList() # if empty linked list return None if not curr: return None else: # if not empty linked list even_head = even_tail = Node(None) odd_head = odd_tail = Node(None) count = 0 while curr: if count == 0: even_tail.next = curr even_tail = even_tail.next elif count == 1: odd_tail.next = curr odd_tail = odd_tail.next curr = curr.next # alternate between 0 and 1 (bitwise operation) count ^= 1 # make sure odd_tail which is at end of final_ll is None # attach odd to even by connecting odd head to even tail # set final linkedlist to even head odd_tail.next = None even_tail.next = odd_head.next final_ll.head = even_head.next return final_ll
def list_pivoting(l:LinkedList,x:int): # 3 nodes with head and iteration pointers(tail) # 1 for less, equal, and greater less_head = less_iter = Node(None) equal_head = equal_iter = Node(None) greater_head = greater_iter = Node(None) # empty linkedlist new_ll=LinkedList() # pointer to input list head curr = l.head # Populates the three lists # based on comparisons to input list # add node to node lists and traverse iteration pointers to corresponding next while curr: if curr.data < x: less_iter.next = curr less_iter = less_iter.next elif curr.data == x: equal_iter.next = curr equal_iter = equal_iter.next else: # curr.data > x. greater_iter.next = curr greater_iter = greater_iter.next curr = curr.next # Combines the three lists. # attach greater to equal and attach equal to less, return greater_iter.next = None equal_iter.next = greater_head.next less_iter.next = equal_head.next new_ll.head = less_head.next 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
while slow != fast: slow = slow.next loopLength += 1 return loopLength if __name__ == "__main__": # 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) node8 = Node(8) node0.next = node1 node1.next = node2 node2.next = node3 node3.next = node4 node4.next = node5 node5.next = node6
curr_1 = curr_1.next # return the new list return new_ll if __name__ == "__main__": # 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)
# 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()
carry = val // 10 return final_ll if __name__ == "__main__": # 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()
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()