[Sword Point offer] Merge two sort lists

Source: Internet
Author: User

Title Description

Input two monotonically increasing list, output two list of linked lists, of course, we need to synthesize the linked list to meet the monotone non-reduction rules.

Enter a description

Two monotone increments of the linked list

Output description

A monotonous and non-decreasing list

Problem analysis

Node declaration:

publicclass ListNode {    int val;    null;    ListNode(int val) {        this.val = val;    }}

Solution One (Recursive) run time: 31ms Memory: 629k

public   Class  Solution {public  listnode merge  ( ListNode List1,listnode list2) {if  (List1==null) return  list2; if  (List2==null )        return  list1;        ListNode mergehead=null ; if  (List1.val<=list2.val)            {mergehead = List1;        Mergehead.next = Merge (LIST1.NEXT,LIST2);            }else  {mergehead = List2;        Mergehead.next = Merge (List1,list2.next);    } return  mergehead; }}

First, determine whether the linked list is empty, and if one is empty, return to the other linked list directly.
Because the linked list is to satisfy the monotony, the value of the two linked list is smaller, the current node of the merged list is assigned, and the next node is recursively.
Returns the current node of the merged list.

Solution two run time: 31ms occupied memory: 629k

 Public classSolution { PublicListNodeMerge(ListNode List1,listnode List2) {if(list1==NULL)returnList2;if(list2==NULL)returnList1; ListNode mlist=NewListNode (0); ListNode Point =mlist; while(list1!=NULL&& list2!=NULL){if(List1.val<=list2.val)             {point.next=list1;           List1=list1.next; }Else{point.next = List2;          List2=list2.next;        } point = Point.next; }if(list1==NULL) {point.next=list2; }Else{point.next=list1; }returnMlist.next; }}

① first, determine whether the linked list is empty, and if one is empty, return to the other linked list directly.

② declare the header and pointer nodes of the merged list

The ③ loop compares the value of the head node of the two list, so that the current point node points to the node with the smaller value, which equals its next node, while the point moves backwards.

④ discriminant Loop, if a list is not traversed, add directly behind the point

⑤ returns the next node of the head node (the header node is the newly created one)

The idea is simple: create a new head node, compare the values of the nodes of the two linked lists sequentially, and concatenate them sequentially with the next pointer.

[Sword Point offer] Merge two sort 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.