Merge two sorted linked lists

Source: Internet
Author: User

Question: merging two sorted one-way linked lists into a linked list requires as little space as possible.

Train of Thought: Typical Merge Sorting ideas. When there are two areas to note: 1. How can we avoid applying for new space for a new linked list? 2. How simple is the code?

The following is my answer. I hope to discuss with you whether the algorithm itself has been optimized? How can I improve the Code style?

#include "stdafx.h"class Node{public:int data;Node* next;Node() : data(-1), next(NULL){}};Node* Merge (Node* head1, Node* head2){Node* head = NULL;Node* ret = NULL;while (head1 || head2){Node* temp = NULL;if (head1 && head2){temp = head1->data < head2->data ? head1 : head2;}else {temp = head1 !=  NULL ? head1 : head2;}if (!head){head = temp;ret = head;}else{head->next = temp;head = head->next;}if (temp == head1){head1 = head1->next;}else{head2 = head2->next;}}head->next = NULL;return ret;}void Print(Node* head){while(head){printf("%d ", head->data);head = head->next;}printf("\n");}void Test0(){Node* node1 = new Node();Node* node2 = new Node();printf("before merge\n");Print(node1);Print(node2);Node* head = Merge(node1, node2);printf("after merge\n");Print(head);}void Test1(){Node* node1 = new Node();Node* head1 = node1;node1->data = 0;Node* node2 = new Node();Node* head2 = node2;node2->data = 1;for (int i = 2; i < 6; i+=2){node1->next = new Node();node1 = node1->next;node1->data = i;node1->next = NULL;node2->next = new Node();node2 = node2->next;node2->data = i + 1;node2->next = NULL;}printf("before merge\n");Print(head1);Print(head2);Node* head = Merge(head1, head2);printf("after merge\n");Print(head);}void Test2(){Node* node1 = new Node();Node* head1 = node1;node1->data = 0;Node* node2 = new Node();Node* head2 = node2;node2->data = 1;for (int i = 2; i <= 6; i+=2){node1->next = new Node();node1 = node1->next;node1->data = i;node1->next = NULL;node2->next = new Node();node2 = node2->next;node2->data = i + 1;node2->next = NULL;}for (int j = 8; j <= 10; j++){node1->next = new Node();node1 = node1->next;node1->data = j;node1->next = NULL;}printf("before merge\n");Print(head1);Print(head2);printf("after merge\n");Node* head = Merge(head1, head2);Print(head);}void Test3(){Node* node1 = new Node();Node* head1 = node1;node1->data = 0;Node* node2 = new Node();Node* head2 = node2;node2->data = 1;for (int i = 2; i <= 6; i+=2){node1->next = new Node();node1 = node1->next;node1->data = i;node1->next = NULL;node2->next = new Node();node2 = node2->next;node2->data = i + 1;node2->next = NULL;}for (int j = 7; j <= 10; j++){node2->next = new Node();node2 = node2->next;node2->data = j;node2->next = NULL;}printf("before merge\n");Print(head1);Print(head2);Node* head = Merge(head1, head2);printf("after merge\n");Print(head);//head1, head2 should do the memory collection job. skip for testing reason.}int _tmain(int argc, _TCHAR* argv[]){Test0();Test1();Test2();Test3();//should be more test like negative value, integer overflow...skip return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.