Question

In: Computer Science

create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify...

create a function to merge and concatenate two linked list l1 =[1,2,4] and l2=[1,3,4]. dont modify l2 (the nodes inserted in l1 should be copies of l2

class Node(object):
def __init__(self, data):
self.data = data
self.next = None
  
class LinkedList(object):
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next


def __init__(self):
self.head = None;
  
def append(self, data):
new_node = Node(data)

if self.head is None:
self.head = new_node
return

last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node

def merge_sorted(self, llist):
  
p = self.head
q = llist.head
s = None
  
if not p:
return q
if not q:
return p

if p and q:
if p.data <= q.data:
s = p
p = s.next
else:
s = q
q = s.next
new_head = s
while p and q:
if p.data <= q.data:
s.next = p
s = p
p = s.next
else:
s.next = q
s = q
q = s.next
if not p:
s.next = q
if not q:
s.next = p
return new_head
def list_concat(self, llist):
current = self.head
while current.next != None:
current = current.next
current.next = llist
return self.head
llist_1 = LinkedList()
llist_2 = LinkedList()

llist_1.append(1)
llist_1.append(2)
llist_1.append(4)

llist_2.append(1)
llist_2.append(3)
llist_2.append(4)


llist_1.list_concat(llist_2)
llist_1.print_list()

this is my code so far. working for merge sort . But confused in writing concat fumction. How to start?

Code is in python

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: The code you provided has indentation messed up, like there is no spaces or tabs before any statements. So I did my best to make it normal. But I’m not sure if I have formatted merge_sorted method correctly. If it is not, ignore it & use your correctly formatted original merge_sorted method. The concat method has been done exactly as needed.

#code

class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None


class
LinkedList(object):
    def print_list(self):
        cur_node = self.head
        while cur_node:
            print(cur_node.data)
            cur_node = cur_node.next

    def __init__(self):
        self.head = None;

    def append(self, data):
        new_node = Node(data)
        if self.head is None:
            self.head = new_node
            return
       
last_node = self.head
        while last_node.next:
            last_node = last_node.next
        last_node.next = new_node

    def merge_sorted(self, llist):
        p = self.head
        q = llist.head
        s = None
        if not
p:
            return q
        if not q:
            return p
        if p and q:
            if p.data <= q.data:
                s = p
                p = s.next
            else:
                s = q
                q = s.next
            new_head = s
        while p and q:
            if p.data <= q.data:
                s.next = p
                s = p
                p = s.next
            else:
                s.next = q
                s = q
                q = s.next
        if not p:
            s.next = q
        if not q:
            s.next = p
        return new_head

    #required method
   
def list_concat(self, llist):
        #taking a reference to the head node of this list
       
last=self.head
        #if last is not the last node, advancing last until
        #it becomes the last node on this list
       
while last!=None and last.next!=None:
            last=last.next
        #taking a reference to the head node of other list
       
current = llist.head
        #looping until current becomes None
       
while current!= None:
            #creating a new node with current's data (copy)
           
new_node = Node(current.data)
            #if last is None, setting as this node's head
           
if last==None:
                self.head=new_node
                #setting this node's head as last, for next iteration
               
last=self.head
            else:
                #otherwise, simply appending as the next of last node
               
last.next=new_node
                #setting new node as the last node, for next iteration
               
last=new_node
            #moving to next node in other list
           
current=current.next
        #returning head of this list
       
return self.head


llist_1 = LinkedList()
llist_2 = LinkedList()

llist_1.append(1)
llist_1.append(2)
llist_1.append(4)

llist_2.append(1)
llist_2.append(3)
llist_2.append(4)

llist_1.list_concat(llist_2)
llist_1.print_list()

#output

1

2

4

1

3

4


Related Solutions

The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list...
The intersection of two lists L1 and L2, L1 ∩ L2, is defined as the list containing elements in both L1 and L2 only. Given two sorted lists L1 and L2, write a function, called intersection, to compute L1 ∩ L2 using only the basic list operations. The intersection function is defined as follows template list intersection( const list & L1, const list & L2); C++
1)Given a list L1, create a list L2 that contains all but the last element of...
1)Given a list L1, create a list L2 that contains all but the last element of L1 (e.g. if L1 is ["a","z",True], L2 should be equal to ["a","z"] 2)Given a string s, assign a variable has_z that is True if s contains the letter "z" anywhere within it. 3)Write Python code that calculates the mean of a given list of numbers x and saves the result in a variable m. 4)Given two numeric variables, x and y, assign the maximum...
Create a F# Function. Function Signature: zip L1 L2 The function zip combines the elements of...
Create a F# Function. Function Signature: zip L1 L2 The function zip combines the elements of two lists pairwise, resulting in a list of tuples, where the first tuple in the list contains the first element of L1 as the first element, and the first element of L2 as the second element // // zip L1 L2 // // Zip two lists // // Returns list of tuples // // Examples: // zip [] [] => [] // zip [1]...
1. Write a Racket function (set-equal? L1 L2) that tests whether L1 and L2 are equal....
1. Write a Racket function (set-equal? L1 L2) that tests whether L1 and L2 are equal. Two sets are equal if they contain exactly the same members, ignoring ordering (or in other words, two sets are equal if they are a subset of each other). For example (set-equal? '(1 (2 3)) '((3 2) 1)) ---> #t (set-equal? '(1 2 3) '((3 2)1)) ---> #f (set-equal? '(1 2 3) '((1 2 3))) ---> #f 2. Two common operations on sets are...
Given two sorted lists L1 and L2, write a procedure to compute L1∪L2 using only the...
Given two sorted lists L1 and L2, write a procedure to compute L1∪L2 using only the basic list operations. Pseudo-code is acceptable.
1) Equations for two lines L1 and L2 are given. Find the angle between L1 and...
1) Equations for two lines L1 and L2 are given. Find the angle between L1 and L2. L1: ? = 7 + 2?, ? = 8 − 4?, ? = −9 + ? L2: ? = −8 − ?, ? = 3 − 3?, ? = 4 + 3? 2) Find polar form of complex number z : ?) ? = 4√3 − 4? ?) ? = 2√3 − 2i
Given two sorted linked lists, merge them into a third sorted linked list. If an element...
Given two sorted linked lists, merge them into a third sorted linked list. If an element is present in both the lists, it should occur only once in the third list. Code needed in java.
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩...
Given two sorted lists, L1 and L2, write an efficient C++ code to compute L1 ∩ L2 using only the basic STL list operations. What is the running time of your algorithm?
C++ Question Create two circular linked lists and find their maximum numbers. Merge the two circular...
C++ Question Create two circular linked lists and find their maximum numbers. Merge the two circular linked lists such that the maximum number of 2nd circular linked list immediately follows the maximum number of the 1st circular linked list. Input: 12 -> 28 -> 18 -> 25 -> 19-> NULL 5 -> 24 -> 12 -> 6 -> 15-> NULL Output: 28 -> 24-> 25 -> 15 -> 19 -> 15->5-> 18 -> 25 -> 19->NULL Note:- Code should work...
concatenate(lst1, lst2 ) Given two (possibly empty) unordered lists, concatenate them such that the first list...
concatenate(lst1, lst2 ) Given two (possibly empty) unordered lists, concatenate them such that the first list comes first.Example: When the first input unordered list lst1 is [1, 2, 3] and the second lst2 is [7,8,9], the outputunordered list is [1,2,3,7,8,9]. Use codes: class Node: def __init__(self, data): self.data = data self.next = None def getData(self): return self.data def getNext(self): return self.next def setData(self, data): self.data = data def setNext(self, node): self.next = node class UnorderedList: def __init__(self): self.head = None...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT