Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Classify delivery styles by type.

Answered: 1 week ago