Question

PYTHON 3 LANGUAGE , LINKED LISTS , define function ITERATIVELY Define an iterative function named alternate_i;...

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def alternate_i(ll1, ll2):
  
   # if first list is empty
   if ll1 is None:
       return ll2
  
   # if second list is empty
   elif ll2 is None:
       return ll1
  
   # if both list are empty
   else:
       return None
      
   prev = None # temp variable
  
   # loop untill both list are empty
   while ll1 not None and ll2 not None:
       prev = ll1.next # store next address of first list
       ll1.next = ll2 # add the current element of second list to first
       ll2 = ll2.next # increase the second list
       ll1.next.next = prev # add the remaining element of first list back
       ll1 = ll1.next.next # increment first list
  
   # if first list contain less no of elements than second add full second list
   if ll2 not None:
       prev.next = ll2
  
   return ll1

Add a comment
Know the answer?
Add Answer to:
PYTHON 3 LANGUAGE , LINKED LISTS , define function ITERATIVELY Define an iterative function named alternate_i;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a) 7% Define a function (the function definition only) named combine_lists that receives two lists of...

    a) 7% Define a function (the function definition only) named combine_lists that receives two lists of integers (the function has 2 parameters). You can assume that each list is the same length and each list is not empty. The function will create a new list which is composed of the addition of each element of the two lists. The function returns the new list. For example, if the function is sent this list: [2,4,6,8,10] and this list: [5,6,7,8,9] then the...

  • I need a python 3 help. Please help me with this question Part 2. Linked Lists...

    I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...

  • 1. Define a function in python that returns the sum of the following 4 lists. Remember...

    1. Define a function in python that returns the sum of the following 4 lists. Remember to store and re- use your code instead of summing the values for each list 4 times. Hence, define a function that takes as an argument a list and returns the sum of values. Copy the following lists and paste them in a Python file after you define the sum_of_values function Call the function and pass to it a different list each call as...

  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item,...

    python Programming assignment: Let's think about doubly-linked lists. Define a class ListNode2, with three attributes: item, left, and rightL. Left link points to the previous node in the list, right link points to the next node in the list. You can also add the display method to this class (like we did it in class for the ListNode class). Then test your class. For example, create a linked list of 5 values: 34,1, 23, 7, and 10. Display it. Then...

  • 117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class....

    117% Question 1: Linked Lists Create an IntLinkedList class, much like the one presented in class. It should implement a linked list that will hold values of type int. It should use an IntNode class to implement the nodes in the linked list. The linked list must have the following methods: . A constructor with no parameters which creates an empty list. . void add (int data) -adds data to the front of the list. .A constructor IntlinkedList(int[]) which will...

  • PYTHON 3 HELP Write a function find_common_elements(list_p,list_q) that accepts two lists, list_p and list_q, as parameters...

    PYTHON 3 HELP Write a function find_common_elements(list_p,list_q) that accepts two lists, list_p and list_q, as parameters and returns a new list that contains only the elements that are common between two lists (without duplicates). Make sure your program works on two lists of different sizes. Demonstrate your function works by calling it with 2 different pairs of lists. Pseudocode for find_common_elements(list_p,list_q): INITIALIZE empty common_list FOR EACH element e in list_p: IF e is in list_q: ADD e to common_list RETURN...

  • Python: Arrays and Lists (do code on repl.it) Part C (4 points) - more advanced lists...

    Python: Arrays and Lists (do code on repl.it) Part C (4 points) - more advanced lists You will need a Python repl to solve part C. In Python, define a function named secondHighest which accepts a list of numbers, and returns the second highest number from the list. If the highest value occurs more than once, then it is also the second highest (see example). Assume the input list will always have length at least two (so there is always...

  • Help me solve this in C++ Rewrite the code to use array instead of linked lists....

    Help me solve this in C++ Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. You can prepare a class if you wish but it is not required. /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max...

  • write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named...

    write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named q3() that accepts a List of characters as a parameter and returns a Dictionary. The Dictionary values will be determined by counting the unique 3 letter combinations created by combining the values at index 0, 1, 2 in the List, then the values at index 1, 2, 3 and so on. The q3() function should continue analyzing each sequential 3 letter combination and using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT