Question
CODE TO IMPORT list_node.py Contains a simple ListNode class, which simply has 'val' and 'next' fields. class ListNode: Models a single node
CODE TO IMPORT
""" list_node.py
Contains a simple ListNode class, which simply has 'val' and 'next' fields. """
class ListNode: """ Models a single node in a singly-linked list. Has no methods, other than the constructor. """
def __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)
8 split list(old head) This function takes a linked list, and splits it into two at the middle. It then returns a tuple, with the heads of the two lists. If the input list has an odd length, then the first returned list must have one more node than the second returned list. The input list - and one or both of the returned lists - may be empty. 8 split list(old head) This function takes a linked list, and splits it into two at the middle. It then returns a tuple, with the heads of the two lists. If the input list has an odd length, then the first returned list must have one more node than the second returned list. The input list - and one or both of the returned lists - may be emptyStep 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