. Merge Sorted Lists-leetcode-java

Source: Internet
Author: User

"The original in the SAE's blog, all transferred to the CSDN. "
. Merge Sorted Lists-leetcode-javaPosted in2016/02/05

Topic

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.

Requires merging of two sequential lists

public class Solution {
Public listnode mergetwolists (listnode L1, ListNode L2) {
listnode dummy= new ListNode ( -1);
ListNode head= Dummy;
//if (L1==null && l2==null) return null;
//if (l1==null) return L2;
//if (l2==null) return L1;
while (L1 = null && L2! = null) {
if (l1.val<l2.val) {
Head.next=l1;
L1 = L1.next;
}else if (l1.val >=l2.val) {//Start here less equals, always null pointer exception, in fact, there is no need for else if, direct else is good.
Head.next=l2;
L2=l2.next;
}
Head=head.next;
}
if (l1!=null && head!=null)
{
head.next=l1;
}
if (l2!=null && head!=null) {
head.next=l2;
}
return dummy.next;
}
}

Related:

1) About ListNode dummy= new ListNode (-1); Here's the-1 explanation .

The node value here can be any value, 5 is OK, 100 is OK, because the value of the node is not used at all.

2) Benefits and implications of using dummy

This problem is often encountered in linked lists: the first node of the list, because there are no precursor nodes, so the node needs special processing, resulting in additional code volume. If you create a dummy, use it as the precursor node of the first, so that all nodes in the list can also be handled with the same logic.

If there is no dummy in this code, it is necessary to add an additional judgment to determine whether the new linked header is Head1 or head2 after merging. And with the dummy later, you can not use this judgment, because whether it is head1 or head2, as long as it is immediately behind the dummy, it must be head1 and head2 the smaller one, this must be the linked list after the joint.

3)

Dummy always records the predecessor node of the combined post-linked header, which is static. The head record is the node in the merge process in which the latest merge comes in, which is dynamic. The zipper can be mended by the brain. Dummy records the initial node of the head of the chain, it is fixed and will not change. But head is always embracing change.

Posted in leetcode|  tags are java, Leetcode| Post a reply

. Merge Sorted Lists-leetcode-java

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.