if node == None: return None dummy_node = ListNode() head = dummy_node while node != None: next = node.next node.next = head.next #None if head.next == None else head.next = node node = next return head.next # def reverse(head): # if head == None or head.next == None: # return head # p = reverse(head.next) # head.next.next = head # 4->5 : 4->5->4 # head.next = None # 4->5->4 : 5->4 # return p if __name__ == "__main__": data = [ [1, 2, 3, 4, 5], [50, 40, 30, 20, 10], [], [3, 2] ] for test_list in data: head = nodelist_builder(test_list) rnode = reverse(head) printNodes(rnode) print("---x---")
# def remove_duplicates(node): # # Using 2 pointers(heads) current and runner # current = node # while current != None: # runner = current # while runner.next != None: # if current.data == runner.next.data: # runner.next = runner.next.next #skip # else: # runner = runner.next # shift ahead # current = current.next def remove_duplicates(node): current = node runner = node.next while runner != None: while current.data == runner.data: runner = runner.next current.next = runner current = current.next runner = runner.next if __name__ == "__main__": data = [1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5] head = nodelist_builder(data) remove_duplicates(head) printNodes(head)
if (node1 == None) and (node2 == None) and (carry == 0): return None result = Node() value = carry if node1 != None: value += node1.data if node2 != None: value += node2.data result.data = value % 10 carry = value // 10 if (node1 != None) or (node2 != None): node = sum_two_list(None if node1 == None else node1.next, None if node2 == None else node2.next, carry) result.next = node return result if __name__ == "__main__": data = [ #([7, 1, 6], [5, 9, 2]) ([6, 1, 7], [2, 9, 5]), ([9, 9], [9, 9]) ] for d in data: head1 = nodelist_builder(d[0]) head2 = nodelist_builder(d[1]) rhead = sum_two_list(head1, head2, 0) #delete_middle_node(head.next.next) printNodes(rhead) print()
([3, 5, 8, 5, 10, 2, 1], 5) def partition(node, threshold): left = node head = node previous = None while head != None: if head.data < threshold: next = head.next if previous: previous.next = next else: previous = head.next = left head = next else: previous = head head = head.next if __name__ == "__main__": data = [ ([10, 1, 2, 3, 4, 2, 4, 5], 3), ([3, 5, 8, 5, 10, 2, 1], 5) ] for test_tuple in data: head = nodelist_builder(test_tuple[0]) new_head = partition(head, test_tuple[1]) printNodes(new_head) print()
dummy_node = ListNode(0) p1 = l1 p2 = l2 carry = 0 curr = dummy_node while p1 != None or p2 != None: x = p1.val if p1 != None else 0 y = p2.val if p2 != None else 0 sum = carry + x + y carry = sum//10 curr.next = ListNode(sum%10) curr = curr.next if p1 != None: p1 = p1.next if p2 != None: p2 = p2.next if carry > 0: curr.next = ListNode(carry) return dummy_node.next if __name__ == "__main__": data = [ ([7, 1, 6], [5, 9, 2]) #([7, 1, 6, 9], [5, 9, 2]) #([9, 9], [9, 9]) ] head1 = nodelist_builder(data[0][0]) head2 = nodelist_builder(data[0][1]) rhead = addTwoNumbers(head1, head2) #delete_middle_node(head.next.next) printNodes(rhead)
from linked_list import ListNode, nodelist_builder, printNodes def merge(l1, l2): if l1 == None and l2 == None: return None dummy_node = ListNode() dummy_head = dummy_node while l1 != None and l2 != None: if l1.val < l2.val: dummy_head.next = l1 #dummy_head = dummy_head.next l1 = l1.next else: dummy_head.next = l2 #dummy_head = dummy_head.next l2 = l2.next dummy_head = dummy_head.next dummy_head.next = l1 if l1 != None else l2 return dummy_node.next if __name__ == "__main__": data = [([1, 2, 4], [1, 3, 4]), ([], []), ([], [0])] for test_list in data: l1 = nodelist_builder(test_list[0]) l2 = nodelist_builder(test_list[1]) m_node = merge(l1, l2) printNodes(m_node) print("---x---")