Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

4.14 Merge Sorted Linked Lists Given two sorted linked lists, your function should merge the two linked links into one. The lists are created for

4.14 Merge Sorted Linked Lists

Given two sorted linked lists, your function should merge the two linked links into one. The lists are created for you, do not modify the function that creates the list. Do not use the following libraries: algorithm, cmath. You do not have to follow the hints given to you in the template. Example Two lists 1->3->5 and 2->3->4->6 will return a list with 1->2->3->3->4->5->6. Input There will be no input read in anymore. Going forward use the hard-coded input on the template and ensure the program works. There will be one compare output test based on the hard-coded input. The rest of the tests will be unit tests. Output Print out the newly returned list. Just print out the integers separated by a space.

#include

#include

// DO NOT MODIFY

struct Node {

int val;

Node * next;

Node( int num ){

val = num;

next = nullptr;

}

};

// DO NOT MODIFY

Node * createList( std::vector & vec ){

int i = 0;

Node * head = new Node( vec[i++] );

Node * runner = head;

for( i; i < vec.size(); i++ ){

Node * temp = new Node( vec[i] );

runner->next = temp;

runner = runner->next;

}

return head;

}

Node * mergeLists(Node * l1, Node * l2) {

/*

Create a dummy node. This node can act as the head of your "new" list. Append the that were already created

to this dummy head. At the end of the function you can return dummyNode->next since your real list will be the

node after the dummy head.

Loop through both lists.

As you loop check the values

Place the node with the lower value first

Make sure both lists have been searched till the end

Return your merged list

*/

}

int main(){

std::vector list1_values { 1, 3, 5 };

std::vector list2_values { 2, 3, 4, 6 };

Node * list1 = createList( list1_values );

Node * list2 = createList( list2_values );

// Save the results from your function

// Print out the list that was returned from your function, put a space between each integer

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions