Question: PYTHON 3 LANGUAGE , LINKED LISTS , define function ITERATIVELY Define an iterative function named alternate_i ; it is passed two linked lists (ll1 and

PYTHON 3 LANGUAGE , LINKED LISTS , define function ITERATIVELY

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).

def alternate_i(ll1 : LN, ll2 : LN) -> LN: # Handle the case of ll1 or ll2 being empty # Set up for iteration (keep track of front and rear of linked list to retur # while True: with both l1 and l2 not empty and ll1 is in the linked list to return while True: break # return correct result if ll1 or ll2 is empty (after advancing ll1 and ll2) # continue looping if both ll1/ll2 are not empty, with ll1 in the linked list to return

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!