Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In pyhon, please. below is the list_node.py 6 accordion recursive (head) Include this function in the file linked_list_recursion_part_2.py . accordion_recursive (head) takes a linked list
In pyhon, please.
below is the list_node.py
6 accordion recursive (head) Include this function in the file linked_list_recursion_part_2.py . accordion_recursive (head) takes a linked list (which might be empty) as its only parameter, and returns a new linked list, where every other node has been removed. The new list must use the same nodes as the original list (don't create any new ones). When removing nodes from the list, you must remove the old head, the node 2 after the old head, and the node 2 after that, out to the end of the list. For instance, if the input list is like this: 10 -> 20 -> -1 -> "asdf" -> 13 -> "qwerty" -> 1024 then the list that you return must look like this: 20 -> "asdf" -> "qwerty" If the old list contained only a single node, or was empty, return an empty list. This function must be recursive, but is not required to obey the Annoying Requirements. 7 pair recursive (head, head2) This function takes two lists, which may be of different lengths (either or both of them might be empty). It builds new ListNode objects, where the value of each one is a tuple, consisting of one value from the first list, and one value from the second list, in the same order as they existed in those input lists. If the lists are different lengths, then the length of the returned list will be equal to the length of the shorter one. This function must be recursive, but is not required to obey the Annoying Requirements. 7.1 EXAMPLE Suppose that the input lists are as follows: headi: 123 -> 456 -> 789 head2: "abc" -> "def" -> "ghi" -> "jkl" then the list that you return must look like this: (123,"abc") -> (456,"def") -> (789,"ghi") 7.2 Recursing on Two Things So far, all of the recursive functions you've done have been over a single thing: either an integer, or a linked list. But there are some problems - like this one - where you recurse over two things in parallel. While this seems weird at first, if you look at it carefully, you'll realize it's not a lot more complex than what you've already done; in this problem you will pair the first value from list 1 with the first value from list 2, and then the second value from list 1 with the second value from list 2, an so on. So really, the only trick here is that when you recurse, you must recurse into the next field of both lists, at the same time. list_node.py Contains a simple ListNode class, which simply has 'val' and 'next' fields. II II II class ListNode: Models a single node in a singly-linked list. than the constructor. Has no methods, other IIIIII def II II 11 init__(self, val): Constructs the object; caller must pass a value, which will be stored in the 'val' field. self.val = val self.next = None def str__(self): vals = [] objs = set() curr = self while curr is not None: curr_str = str(curr.val) if curr in objs: vals.append("{} -> ... (to infinity and beyond)".format(curr_str break else: vals.append(curr_str) objs.add(curr) curr = curr.next return " -> ".join(vals) 6 accordion recursive (head) Include this function in the file linked_list_recursion_part_2.py . accordion_recursive (head) takes a linked list (which might be empty) as its only parameter, and returns a new linked list, where every other node has been removed. The new list must use the same nodes as the original list (don't create any new ones). When removing nodes from the list, you must remove the old head, the node 2 after the old head, and the node 2 after that, out to the end of the list. For instance, if the input list is like this: 10 -> 20 -> -1 -> "asdf" -> 13 -> "qwerty" -> 1024 then the list that you return must look like this: 20 -> "asdf" -> "qwerty" If the old list contained only a single node, or was empty, return an empty list. This function must be recursive, but is not required to obey the Annoying Requirements. 7 pair recursive (head, head2) This function takes two lists, which may be of different lengths (either or both of them might be empty). It builds new ListNode objects, where the value of each one is a tuple, consisting of one value from the first list, and one value from the second list, in the same order as they existed in those input lists. If the lists are different lengths, then the length of the returned list will be equal to the length of the shorter one. This function must be recursive, but is not required to obey the Annoying Requirements. 7.1 EXAMPLE Suppose that the input lists are as follows: headi: 123 -> 456 -> 789 head2: "abc" -> "def" -> "ghi" -> "jkl" then the list that you return must look like this: (123,"abc") -> (456,"def") -> (789,"ghi") 7.2 Recursing on Two Things So far, all of the recursive functions you've done have been over a single thing: either an integer, or a linked list. But there are some problems - like this one - where you recurse over two things in parallel. While this seems weird at first, if you look at it carefully, you'll realize it's not a lot more complex than what you've already done; in this problem you will pair the first value from list 1 with the first value from list 2, and then the second value from list 1 with the second value from list 2, an so on. So really, the only trick here is that when you recurse, you must recurse into the next field of both lists, at the same time. list_node.py Contains a simple ListNode class, which simply has 'val' and 'next' fields. II II II class ListNode: Models a single node in a singly-linked list. than the constructor. Has no methods, other IIIIII def II II 11 init__(self, val): Constructs the object; caller must pass a value, which will be stored in the 'val' field. self.val = val self.next = None def str__(self): vals = [] objs = set() curr = self while curr is not None: curr_str = str(curr.val) if curr in objs: vals.append("{} -> ... (to infinity and beyond)".format(curr_str break else: vals.append(curr_str) objs.add(curr) curr = curr.next return " -> ".join(vals)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started