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

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...
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 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?
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...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
For the Poincare plane, find two lines L1 and L2 and a point P off each...
For the Poincare plane, find two lines L1 and L2 and a point P off each such that through P there are exactly two lines parallel to both L1 and L2.
ABC ltd is a circuit board producer and produces two products, L1 & L2. The company...
ABC ltd is a circuit board producer and produces two products, L1 & L2. The company currently uses traditional costing system for making business decisions. However, recently the company is considering a move towards the Activity-Based Costing (ABC) system. As one of the company’s accountant’s you have been asked to prepare a statement comparing both the costing methods for the next company board meeting. Your supervisor has given you the following quarterly data: - Total Indirect Costs for the quarter:...
Modify this linked list code to work with string. Insert the following items into the list...
Modify this linked list code to work with string. Insert the following items into the list and display the list. The items are: Pepsi, Coke, DrPepper, Sprite, Fanta. Insert them in that order. Display the list. Then delete DrPepper and redisplay the list. Then insert 7-UP and redisplay the list. Then append Water and redisplay the list. c++ ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ #include <iostream> using namespace std; class ListNode { public:     int value;     ListNode *next;     ListNode(int nodeValue) {       value...
In python i want to create a function. The function will take in two linked lists...
In python i want to create a function. The function will take in two linked lists as the parameters. If one is shorter than the other then the shorter will be the length. I want to take the values from both linked lists and turn them into tuples. I then want these tuples to be put into a new linked list. I want to return that linked list. I want to do this using recursion and no helper functions or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT