21. Merge Two Sorted Lists, mergesorted

Source: Internet
Author: User

21. Merge Two Sorted Lists, mergesorted

The question comes from leetcode. The requirement is to splice two sorted linked lists into an ordered linked list. The linked list structure is as follows:

public class ListNode{        int val;        ListNode next;        ListNode(int x){            val=x;        }    }

As follows, a rectangle becomes a node, and a linked list has at least one node.

Method 1: first create a new linked list L, compare the Val of the current L1 and L2 nodes, add a small one to the L, and move the node back,

Until a linked list is empty. Add another non-empty linked list to L. The time complexity is O (n + m ).

Header: the Header node of the new linked list. pre is the last node of the current linked list. The specific steps are as follows:

1. Check whether one of L1 and L2 is null. If there is null, execute 3. Otherwise, execute 2.

2. compare the Val values of L1 and L2, add a small value to the back of the pre node, and move a node (ListNode = ListNode. next, note that the ListNode of the linked list has changed ),

At the same time, pre points to the last Header linked list. Run 1.

3. If L1 is not empty, L1 is added to the end of pre. If L2 is not empty, L2 is added to the end of the pre node.

The specific implementation code is as follows:

public ListNode mergeTwoLists(ListNode l1, ListNode l2){        ListNode header=new ListNode(0);        ListNode pre=header;        while(l1!=null&&l2!=null){            if(l1.val>l2.val){                pre.next=l2;                l2=l2.next;            }else{               pre.next=l1;               l1=l1.next;            }            pre=pre.next;        }        if(l2!=null){            pre.next=l2;        }        if(l1!=null){            pre.next=l1;        }        return header.next;    }

Method 2: Because L1 and L2 are already ordered, you can consider inserting L2 into L1. Here we only talk about how to perform the insert operation. The specific process depends on the java code.

    

The Header is the Header node, and the pre is the end node of the current node. The red line indicates the point to be modified, and the green line indicates the point to be deleted.

Now, when L2 Val is smaller than L1,

The insert procedure is as follows:

1. Retained L2 next node as next

2. Modify the next node of L2 to point to the next node of pre.

3. Modify the pre-directed node to L2.

4. Move pre and L2 to the right of each node.

The Code is as follows:

public ListNode mergeTwoLists(ListNode l1, ListNode l2){        ListNode helper=new ListNode(0);        ListNode pre=helper;        helper.next=l1;        while(l1!=null&&l2!=null){            if(l1.val>l2.val){                ListNode next=l2.next;                l2.next=pre.next;                pre.next=l2;                l2=next;            }else{                l1=l1.next;            }            pre=pre.next;        }        if(l2!=null){            pre.next=l2;        }        return helper.next;    }

Method 3: recursive thinking is used. If a function is regarded as a small node of the two linked lists, the recursion end is null for one of the two linked lists.

Step 1: 1. Create a node with a smaller header of L1 and L2

2. Create a node where the nonheader of the intermediate variable is large in L1 and L2.

· 3. Point the next node of the header to the returned result of the recursive function. However, at this time, the header in the passing parameter has shifted a unit to the right.

   

The Code is as follows:

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {        if(l1==null) return l1;        if(l2==null) return  l2;        ListNode head=(l1.val<l2.val)?l1:l2;        ListNode nonhead=(l1.val<l2.val)?l2:l1;        head.next=mergeTwoLists(head.next,nonhead);        return  head;    }

All three methods can be implemented, and the recursive method is the best in the overall effect. If any, I hope you can help me.

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.