Sword Point of Offer (Java Edition): Merge two sorted lists

Source: Internet
Author: User

Title: Enter two incrementally sorted lists, merge the two linked lists, and make the nodes in the new list continue to be sorted in ascending order. For example, the list of links in the input graph 1 and linked List 2, the merge after the ascending chain is shown in table 3.


This is a frequently used by the company's face questions. During the interview process, we found that the candidate is the most likely to make two mistakes: first, before writing the code to think about the merger process, the resulting merged list is either broken in the middle or not in an incremental order, and the robustness of the code is problematic, once the program has a special input (such as an empty list) will crash. Next analyze how to solve this problem.

First, the process of merging two linked lists is analyzed. Our analysis starts with merging the head nodes of the two linked lists. The value of the head node of the list 1 is less than the value of the head node of the linked list 2, so the head node of the linked list 1 is the head node of the merged list:


We continue to merge the remaining nodes in the two linked lists. The remaining nodes in the two lists are still sorted, so the steps to merge the two lists are the same as in the previous steps. We still compare the values of two header nodes. At this point, the value of the head node of the list 2 is less than the value of the head node of the linked list 1, so the value of the head node of the list 2 will be the head node of the linked list that the remaining nodes are merged. We link this node with the tail node of the linked list when the linked list was previously merged.

When we get the smaller head node in the two list and link it to the linked list, the remaining nodes of the two lists are still sorted, so the merging steps are the same as the previous steps. This is the typical recursive process, in which we suspect that a recursive function is defined to complete this merging process.

Next we will solve the problem of robustness. The program crashes whenever the code tries to access the memory that the null pointer points to, resulting in robustness issues. Once the empty list is entered in the subject, an empty pointer is introduced, so we have to deal with the empty list separately. When the first linked list is an empty list, that is, its head node is a null pointer, then it is merged with the second linked list, and the process of the idler merge is the second linked list. Similarly, when the head node of the second linked list is a null pointer, we combine it with the first linked list to get the first linked list. If two linked lists are empty lists, the result of merging is to get an empty list.

Here is the implementation of the Java code:

/** * Enter two increments of the linked list, merge the two linked lists and make the nodes in the new list continue to be sorted in ascending order. */package Swordforoffer;import Utils. listnode;/** * @author Jinshuangqi * * August 1, 2015 */public class E17mergesortedlists {public ListNode  Merge (ListNode PH Ead1,listnode pHead2) {if (pHead1 = = null) return Phead2;else if (pHead2 = = null) return pHead1; ListNode pmergedhead = null;if (Phead1.data <phead2.data) {pmergedhead = Phead1;pmergedhead.next = Merge (PHead1.next, PHEAD2);} Else{pmergedhead = Phead2;pmergedhead.next = Merge (Phead1,phead2.next);} return pmergedhead;}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sword Point of Offer (Java Edition): Merge two sorted lists

Related Article

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.