Reference no: EM132795719
Task
In this practice, we will be implementing a join function for two linked list. You are required to use python. For help you can use the lecture slides.
Task:
Question 1. Given the code for the following linked list class
class LinkedList(object):
def __init__(self):
# head of list self.head = None
# Linked list Node class Node(object):
def __init__(self, d): self.data = d self.next = None
def insert_at_head(self, value): new_node = self.Node(value) new_node.next = self.head self.head = new_node
def joinList(self, q):
######### Write your code here######
#####################################
# Function to print linked list def printList(self):
temp = self.head while temp != None:
print(str(temp.data))
temp = temp.next print(‘ ')
Complete the joinList method such that function that inserts nodes of linked list q at alternate position of the list pointed by self. For example,
Case 1: Size of both list is same
If P is a list containing 1->3->5->7->9 and q is another list containing 2->4->6-
>8->10 then the P.join(q) method will
a. Update the list P to 1->2->3->4->5->6->7->8->9->10 (elements of q
is inserted at alternate locations of P)
b. List q will become null
ii. Case 2: Size of q > size of P
If P is a list containing 1->3->5 and q is another list containing 2->4->6-
>8->10 then the P.join(q) method will
a. Update the list P to 1->2->3->4->5->6 (elements of q is inserted at alternate locations of P)
b. List q will be 8->10
iii. Case 3: Size of q < size of P
If P is a list containing 1->3->5->7->9 and q is another list containing 2-
>4->6 then the P.join(q) method will
a. Update the list P to 1->2->3->4->5->6->7->9 (elements of q is inserted at alternate locations of P)
b. List q will be null
Your implementation should have a time complexity is O(n) where n is number of nodes in first list.
Question 2. Write a code to test your implementation for various P and q
Attachment:- Practice.rar