def insert(self,value): new_node = ln.ListNode(value) new_node.next = None if self.head == None and self.tail == None: self.head = new_node self.tail = new_node else: self.tail.next = new_node self.tail = self.tail.next
def build_linked_list(data): """A helper function for linked list parser. Constructs a linked list from a list of values. :param data - list of values. """ head = None for x in reversed(data): head = list_node.ListNode(x, head) return head
def merge_two_sorted_lists(L1, L2): temp_head = temp_tail = node.ListNode() while L1 and L2: if L1.data < L2.data: temp_tail.next = L1 L1 = L1.next else: temp_tail.next = L2 L2 = L2.next temp_tail = temp_tail.next temp_tail.next = L1 or L2 return temp_head.next
def reverse_sublist(L, start, finish): # TODO - you fill in here. dummy_head = sublist_head = list_node.ListNode(0, L) # traverse to beginning of sublist for _ in range(1, start): sublist_head = sublist_head.next # traverse and reverse sublist sublist_iter = sublist_head.next # current end of traversed list for _ in range(finish - start): temp = sublist_iter.next # this one flips to front sublist_iter.next, temp.next, sublist_head.next = \ (temp.next, # becomes last node in traversed sublist sublist_head.next, # point to current beginning temp # end now becomes front ) return dummy_head.next
#! /usr/bin/python3 """ Code to test the split_list() function Authors: Russ Lewis (based, very vaguely, on older code by Saumya Debray) """ import list_node import proj05_short ########################################################### # INPUT AND EXPECTED OUTPUT # ########################################################### # INPUT: # First, we build a list of nodes. # Then we chain them together. nodes = [list_node.ListNode(2)] in_list = nodes[0] # EXPECTED OUTPUT: # The *nodes* that we expect as the output list. Note that we don't need to # explicitly check that the contents are correct (and unchanged in each # node), since the list-printout will implicitly check that. But we *do* # want to assert that the *nodes* are in exactly the order we expect. If # they aren't (but we pass the value check), then this means that the # student was moving values around between nodes (or, creating new nodes). expected_out_list1 = nodes[:(len(nodes) + 1) // 2] expected_out_list2 = nodes[(len(nodes) + 1) // 2:] ########################################################### # TEST CODE #
""" Code to test the accordion_n() function Authors: Russ Lewis (based, very vaguely, on older code by Saumya Debray) """ import list_node import proj05_long ########################################################### # INPUT AND EXPECTED OUTPUT # ########################################################### # INPUT: # First, we build a list of nodes. # Then we chain them together. nodes = [ list_node.ListNode(2), list_node.ListNode(3), list_node.ListNode(5), list_node.ListNode(7), list_node.ListNode(11), list_node.ListNode(13), list_node.ListNode(17), list_node.ListNode(19), list_node.ListNode("abc"), list_node.ListNode("def"), list_node.ListNode("ghi"), list_node.ListNode("jkl"), list_node.ListNode("mno"), list_node.ListNode("pqrs"), list_node.ListNode("tuv"), list_node.ListNode("wxyz"),
""" Code to test the list_to_array() function Authors: Russ Lewis (based, very vaguely, on older code by Saumya Debray) """ import list_node import proj05_short ########################################################### # INPUT AND EXPECTED OUTPUT # ########################################################### # INPUT: # First, we build a list of nodes. # Then we chain them together. nodes = [ list_node.ListNode(2), list_node.ListNode(3), list_node.ListNode(5), list_node.ListNode(7), list_node.ListNode(11), list_node.ListNode(13), list_node.ListNode(17), list_node.ListNode(19) ] in_list = nodes[0] nodes[0].next = nodes[1] nodes[1].next = nodes[2] nodes[2].next = nodes[3] nodes[3].next = nodes[4] nodes[4].next = nodes[5] nodes[5].next = nodes[6]
temp_head = temp_tail = node.ListNode() while L1 and L2: if L1.data < L2.data: temp_tail.next = L1 L1 = L1.next else: temp_tail.next = L2 L2 = L2.next temp_tail = temp_tail.next temp_tail.next = L1 or L2 return temp_head.next if __name__ == "__main__": L1 = node.ListNode(2) node2 = node.ListNode(3) node3 = node.ListNode(5) node4 = node.ListNode(7) L1.next = node2 node2.next = node3 node3.next = node4 L2 = node.ListNode(1) node_2 = node.ListNode(4) node_3 = node.ListNode(6) L2.next = node_2 node_2.next = node_3
""" import list_node import proj05_short ########################################################### # INPUT AND EXPECTED OUTPUT # ########################################################### # INPUT: # First, we build two lists of nodes. # Then we chain each together. nodes1 = [ list_node.ListNode(2), list_node.ListNode(3), list_node.ListNode(5), list_node.ListNode(7), list_node.ListNode(11), list_node.ListNode(13), list_node.ListNode(17), list_node.ListNode(19) ] nodes2 = [ list_node.ListNode("abc"), list_node.ListNode("def"), list_node.ListNode("ghi"), list_node.ListNode("jkl"), list_node.ListNode("mno"), list_node.ListNode("pqrs"), list_node.ListNode("tuv"),
Authors: Russ Lewis (based, very vaguely, on older code by Saumya Debray) """ import list_node import proj05_long # INPUT: # First, we build a list of nodes. # Then we chain them together. vals1 = [-35, 74, 32, 22, -28, 38, 78, 15, 18, 5, 30, -4, 63, 45, -27, 41] vals2 = [ 'vbp', 'icn', 'hxh', 'vjb', 'skf', 'xml', 'lgs', 'iic', 'whx', 'puj', 'gxf', 'cru', 'enx', 'emw', 'rwp', 'arq', 'vrx', 'btx', 'ger', 'kjh' ] nodes1 = [list_node.ListNode(val) for val in vals1] nodes2 = [list_node.ListNode(val) for val in vals2] in_list1 = nodes1[0] for i in range(len(vals1) - 1): nodes1[i].next = nodes1[i + 1] in_list2 = nodes2[0] for i in range(len(vals2) - 1): nodes2[i].next = nodes2[i + 1] ########################################################### # TEST CODE # ########################################################### def main():