if current1: value = current1.data + accumulator output.insert_at_end(Node(None, value % 10)) if value > 10: output.insert_at_end(Node(None, int(math.floor(value/10)))) elif current2: value = current2.data + accumulator output.insert_at_end(Node(None, value % 10)) if value > 10: output.insert_at_end(Node(None, int(math.floor(value/10)))) else: if accumulator > 0: output.insert_at_end(Node(None, accumulator)) return output def reverse_list(lst): node = Node(None, lst.root.data) current = lst.root.next while current: node = Node(node, current.data) current = current.next return LinkedList(node) lst1 = LinkedList.build_from_array([5,2,5]) lst2 = LinkedList.build_from_array([1,4,8,5]) add_lists_reverse(lst1, lst2).prn() add_lists_reverse(reverse_list(lst1), reverse_list(lst2)).prn()
from linked_list import LinkedList, Node def reverse_list(lst): node = Node(None, lst.root.data) current = lst.root.next while current: node = Node(node, current.data) current = current.next return LinkedList(node) def check_palindrome(lst): lst2 = reverse_list(lst) current1 = lst.root current2 = lst2.root while current1 and current2: if current1.data != current2.data: return False current1 = current1.next current2 = current2.next return True lst1 = LinkedList.build_from_array([1,2,3,2,1]) lst2 = LinkedList.build_from_array([1,2,3,1,1]) print(check_palindrome(lst1)) print(check_palindrome(lst2))
previous = lst.root current = lst.root while current: if current.data in hsh: previous.next = current.next else: hsh[current.data] = True previous = current current = current.next def remove_dupes_in_place(lst): current = lst.root while current: runner = current.next previous = current while runner: if runner.data == current.data: previous.next = runner.next else: previous = previous.next runner = runner.next current = current.next lst = LinkedList.build_from_array(['F', 'O', 'L', 'L', 'O', 'W', 'U', 'P']) lst.prn() remove_dupes_in_place(lst) lst.prn() lst = LinkedList.build_from_array(['F', 'O', 'L', 'L', 'O', 'W', 'U', 'P']) remove_dupes(lst) lst.prn()
from linked_list import LinkedList def delete_in_middle(node): if not node.next: return node.data = node.next.data node.next = node.next.next lst = LinkedList.build_from_array([1, 2, 3, 4]) lst.prn() delete_in_middle(lst.root.next.next) lst.prn()
from linked_list import LinkedList, Node def partition_list(num, lst): smaller = LinkedList(None) bigger = LinkedList(None) current = lst.root while current: print('hi') print(current.data <= num) smaller.insert_at_end(Node(None, current.data)) if current.data <= num else bigger.insert_at_end(Node(None, current.data)) current = current.next smaller.insert_at_end(bigger.root) return smaller lst = LinkedList.build_from_array([3, 4, 7, 2, 6, 1, 5, 8, 9, 5]) lst.prn() partition_list(5, lst).prn()