[Leetcode]21.merge Sorted Lists

Source: Internet
Author: User

"title"

Merge sorted linked lists and return it as a new list. The new list should is made by splicing together the nodes of the first of the lists.

"Analysis"

No

"Code"

/********************************** Date: 2015-01-06* sjf0115* title: 21.Merge, Sorted lists* Source: Https://oj.leetcode. com/problems/merge-two-sorted-lists/* Result: ac* Source: leetcode* Blog: **********************************/#include <    iostream>using namespace std;struct listnode{int val;    ListNode *next;        ListNode (int x): Val (x), Next (NULL) {}};class solution {Public:listnode *mergetwolists (ListNode *l1, ListNode *l2) {        ListNode *head = new ListNode (-1);        ListNode *p1 = L1,*P2 = L2,*cur = head;                while (P1 = null && P2! = null) {//Take two smaller elements of the list header if (P1->val < P2->val) {                Cur->next = p1;            P1 = p1->next;                }//if else{cur->next = p2;            P2 = p2->next;        }//else cur = cur->next;            }//while//If the list 1 is not finished traversing while (p1) {cur->next = P1;            P1 = p1->next; Cur = cur-> next;            }//while//If the list 2 is not finished traversing while (p2) {cur->next = p2;            P2 = p2->next;        Cur = cur->next;        }//while cur->next = NULL;    Return head->next;    }};int Main () {solution solution;    int a[] = {1,2,4,7,9};    int b[] = {3,5,8,10,11,12};    List 1 ListNode *head1 = new ListNode (a[0]);    ListNode *p1 = head1;        for (int i = 1;i < 5;i++) {ListNode *node = new ListNode (A[i]);        P1->next = node;    p1 = node;    }//for//List 2 ListNode *head2 = new ListNode (b[0]);    ListNode *p2 = head2;        for (int i = 1;i < 6;i++) {ListNode *node = new ListNode (B[i]);        P2->next = node;    p2 = node;    }//for ListNode *head = solution.mergetwolists (head1,head2);    Output ListNode *p = head;        while (p) {cout<<p->val<< "";    p = p->next; }//while Cout<<endl;}



"Code Two"

Recursive implementation

Class Solution {public:    listnode *mergetwolists (ListNode *l1, ListNode *l2) {        if (L1 = = NULL) return l2;        if (L2 = = NULL) return L1;        if (L1->val < L2->val) {            L1->next = mergetwolists (L1->next, L2);            return L1;        } else {            L2->next = mergetwolists (L2->next, L1);            return l2;}}    ;





[Leetcode]21.merge Sorted Lists

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.