Question

Given two sorted lists, L1 and L2, write a C++ function to compute L1 ∪ L2...

Given two sorted lists, L1 and L2, write a C++ function to compute L1 ∪ L2 using only the basic list operations.

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

#include <bits/stdc++.h>
using namespace std;

struct LLNode{
   int data;
   LLNode *next;  
};
void insertAtBeginning(LLNode**head,int dataToBeInserted);
//search function, says whether the element is present in that list or not
int search(LLNode *head, int data)
{
LLNode* temp = head;
while (temp != NULL) {
if (temp->data == data){
return 1;
}
temp = temp->next;
}
return 0;
}
//Finding the union of elements in two linked lists
//By checking whether the element in list2 is present in list1 or not
LLNode* findUnion(LLNode* list1, LLNode* list2)
{
LLNode* unionLL = NULL;
LLNode* temp1 = list1;
LLNode* temp2 = list2;
//Inserting all the elements in list1 to unionLL list
while(temp1 != NULL) {
insertAtBeginning(&unionLL, temp1->data);
temp1 = temp1->next;
}
//If element in list2 is not present in list1
//then inserting the element to unionLL list
while(temp2 != NULL) {
if (!search(list1, temp2->data))
{
insertAtBeginning(&unionLL, temp2->data);
}
temp2 = temp2->next;
}
return unionLL;
}

Add a comment
Know the answer?
Add Answer to:
Given two sorted lists, L1 and L2, write a C++ function to compute L1 ∪ L2...
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
  • Given two sorted lists, L1 and L2, write a procedure to computeL1 ∪ L2 using only...

    Given two sorted lists, L1 and L2, write a procedure to computeL1 ∪ L2 using only the basic list operations.

  • You have two sorted lists of integers, L1 and L2. You know the lengths of each...

    You have two sorted lists of integers, L1 and L2. You know the lengths of each list, L1 has length N1 and L2 has length N2. (a) Design an efficient algorithm (only pseudocode) to output a sorted list L1 ∩ L2 (the intersection of L1 and L2). (b) If you know that N2 > N1. What is the running time complexity of your algorithm? Justify. Important Note: For this problem, you don’t need to submit any implementation in Java. Only...

  • c++ Write pseudocode that merges two sorted lists into a new third sorted list by using...

    c++ Write pseudocode that merges two sorted lists into a new third sorted list by using only ADT sorted list operations.

  • Data Structure C++ Write pseudocode that merges two sorted lists into a new third sorted list...

    Data Structure C++ Write pseudocode that merges two sorted lists into a new third sorted list by using only ADT sorted list operations.

  • PYTHON QUESTION 2. Write a function called first_day_greater that takes two lists, L1 and L2 ,...

    PYTHON QUESTION 2. Write a function called first_day_greater that takes two lists, L1 and L2 , representing the daily measured weights of rat 1 and rat 2, respectively, and returns the index of the first day for which the weight for the fist rat is greater than the weight of the second rat.If there are no such days then the function should return -1. You may NOT assume that L1 and L2 are the same length. Use the following to...

  • 1)Given two lists L1 and L2, create a new list L3 consisting of the first element...

    1)Given two lists L1 and L2, create a new list L3 consisting of the first element of L1 and the last element of L2. For example, if L1=[1,2,3] and L2=["a","b","c"], L3 should be [1,"c"] 2)given a string s, create a new string odd_letters that contains only the odd letters of s (two ways to solve this problem are to use slicing, or a while or for loop), i.e. starting at the first letter or the zeroth index: if s="banana", odd_letters...

  • PYTHON 3. Write code using zip and filter so that these lists (l1 and l2) are...

    PYTHON 3. Write code using zip and filter so that these lists (l1 and l2) are combined into one big list and assigned to the variable opposites if they are both longer than 3 characters each. l1 = ['left', 'up', 'front'] l2 = ['right', 'down', 'back']

  • Problem #1: (15 points) You have two sorted lists of integers, L, and L2. You know...

    Problem #1: (15 points) You have two sorted lists of integers, L, and L2. You know the lengths of each list, L1 has length N, and L2 has length N2 (a) Design an efficient algorithm (only pseudocode) to output a sorted list L L2 (the intersection of L and L2). (b) If you know that N2> Ni. What is the running time complexity of your algorithm? Justify Important Note For this problem, you don't need to submit any implementation in...

  • Question 3 (13] (a) Write a procedure filter (L,PredName, L1,L2) that accepts a list L and...

    Question 3 (13] (a) Write a procedure filter (L,PredName, L1,L2) that accepts a list L and returns two lists Li and L2, where Li contains all elements in L for which PredName (x) fails, and L2 contains all elements in L for which PredName (x) succeeds. The predicate PredName/1 should be defined when calling the procedure filter. For example: let test be defined as test(N).- N > 0. 7- filter((-6,7,-1,0),test,L1,L2). L1 - (-6.-1) L2 - [7, 0] NB Use the...

  • Could somebody please help. Thanks in advance! Merge two sorted linked lists and return it as...

    Could somebody please help. Thanks in advance! Merge two sorted linked lists and return it as a new list. 1) Implement a method: mergeTwoLists(…){…} 2) Use the LinkedList library in Java 3) Test your method. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 ````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````` import java.util.*; public class Inclass1 { public static void main(String[] args){ //Question 5.2 LinkedList l1 = new LinkedList<>(); LinkedList l2 = new LinkedList<>(); l1.add(1); l1.add(2); l1.add(10); l2.add(1); l2.add(3); l2.add(4); LinkedList l3 = mergeTwoLists(l1, l2); System.out.println("Question \n******************************"); for(int i...

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