OJ Practice 10--t21 Merge, Sorted Lists

Source: Internet
Author: User

The linked list structure is given by merging the ordered linked list.

The linked list required to be returned is made up of the nodes of the original linked list, and no more nodes are recreated.

Ideas

Data structure entry algorithm. Set "Slider" for two linked lists, compare the size of the current slider value, and the small one will return the end of the linked list pointer to it.

Attention:

1. To set up a flag that always points to its tail node for the return list, it is convenient to enter the new node.

2. Consider the case where the original linked list is empty.

"My Code"

The writing is rather stupid, but it is always right. worthy of encouragement.

structListNode {intVal; ListNode*Next; ListNode (intx): Val (x), Next (NULL) {}}; ListNode*mergetwolists (ListNode *l1, ListNode *L2) {ListNode*head=NULL; ListNode*p=NULL; ListNode*q=NULL; if(l1==null&&l2==NULL)returnNULL; Else if(l1==NULL)returnL2; Else if(l2==NULL)returnL1; if(l1->val<l2->val) {Head=L1; P=l1->Next; Q=L2; }    Else{Head=L2; P=L1; Q=l2->Next; } ListNode*r=Head;  while(p!=null&&q!=NULL) {        if(p->val<q->val) {R->next=p; P=p->Next; R=r->Next; }        Else{R->next=Q; Q=q->Next; R=r->Next; }    }    if(p!=NULL) R->next=p; if(q!=NULL) R->next=Q; returnhead;}

"Other Code"

ListNode *mergetwolists (ListNode *l1, ListNode *L2) {ListNode*helper=NewListNode (0);// pay attention here! Switching to ListNode *helper=null is not feasible ListNode*head=Helper;  while(L1 &&L2) {             if(L1->val<l2->val) helper->next=l1,l1=l1->Next; ElseHelper->next=l2,l2=l2->Next; Helper=helper->Next; }        if(L1) helper->next=L1; if(L2) helper->next=L2; returnHead->Next; }

"Value for the rating"

1. Because the incoming parameter is not a reference and const, the actual incoming copy is not affected by the modification of the original linked list. So there is no need to set up a pointer p,q, directly with L1,L2.

2. The idea of eliminating empty chains is too cumbersome. Note where the callout is, the helper is assigned a node, initializes its val=0, and its next default is initialized to null, so when L1,L2 is null, it is no problem to return Head->next.

If Hepler=null, an error is returned. Here the helper acts as the head's tail node pointer.

PostScript

This was the first time that I made it all by myself in minutes.

Though there is a long-to go, it's still a encouragement for me.

OJ Practice 10--t21 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.