Question: Python pls (Also this function should be iterator function) Define an iterative function named alternate_i; it is passed two linked lists (ll1 and ll2) as
Python pls (Also this function should be iterator function)
Define an iterative function named alternate_i;
it is passed two linked lists (ll1 and ll2) as arguments. It returns a reference to the front of a linked list that alternates the LNs from ll1 and ll2, starting with ll1;
if either linked list becomes empty, the rest of the LNs come from the other linked list. So, all LNs in ll1 and ll2 appear in the returned result (in the same relative order, possibly separated by values from the other linked list).
The original linked lists are mutated by this function (the .nexts are changed; create no new LN objects).
For example, if we defined a = list_to_ll(['a', 'b', 'c',]) and b = list_to_ll([1,2,3,4,5]) alternate_i(a,b) returns a->1->b->2->c->3->4->5->None and alternate_i(b,a) returns 1->a->2->b->3- >c->4->5->None. You may not create/use any Python data structures in your code: use linked list processing only. Change only .next attributes (not .value attributes).
class LN: def __init__(self,value,next=None): self.value = value self.next = next
def list_to_ll(l): if l == []: return None front = rear = LN(l[0]) for v in l[1:]: rear.next = LN(v) rear = rear.next return front
def str_ll(ll): answer = '' while ll != None: answer += str(ll.value)+'->' ll = ll.next return answer + 'None'
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
