21. Merge Two Sorted Lists-leetcode-java

【原来在SAE的blog上,都转到CSDN了。。】

21.
Merge Two Sorted Lists-leetcode-java

发表于 2016/02/05

题目

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two 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){ //一开始这里少了等于,老是空指针异常,其实这里不需要else if了,直接else就好。

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;

}

}

相关:

1)关于加黑的ListNode dummy= new ListNode(-1);这里是-1的解释

这里的节点值可以是任意值,5也行,-100也行,因为该节点的值后面根本就没有用到。

2)使用dummy的好处 和意义所在

链表题中经常会遇到这样的问题:链表的第一个node,因为没有前驱节点,所以该node需要特殊处理,会导致额外的代码量。如果创建一个dummy,将其作为第一个node的前驱节点,这样链表中所有的node都可以也能够同样的逻辑来处理了。

这段代码中如果没有dummy,那么要确定合并后的新链表头到底是head1还是head2,就必须加一个额外判断。而有了dummy以后,则可以不用这个判断,因为不管是head1还是head2,只要是紧接在dummy后面的,肯定就是head1和head2中较小的那个,这个一定是合并后的链表头。

3)

dummy始终记录的是合并后链表头的前驱节点,是静态的。而head记录的是合并过程中最新merge进来的节点, 是动态的。可以脑补一下拉链。dummy记录上了最初的链表头的前驱节点后,就固定了,不会变啦。但是head是一直在拥抱变化。

发表在 leetcode
| 标签有 javaleetcode
发表回复

时间: 2024-08-03 23:54:11

21. Merge Two Sorted Lists-leetcode-java的相关文章

Merge Two Sorted Lists leetcode java

题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题解: 这道题是链表操作题,题解方法很直观. 首先,进行边界条件判断,如果任一一个表是空表,就返回另外一个表. 然后,对于新表选取第一个node,选择两个表表头最小的那个作为新表表头,指针后挪. 然后同时遍历

Merge k Sorted Lists leetcode java

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解: Merge k sorted linked list就是merge 2 sorted linked list的变形题. 而且我们很自然的就想到了经典的Merge Sort,只不过那个是对数组进行sort.而不同的地方,仅仅是Merge两个list的操作不同. 这里来复习一下Merge

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example Example: Input: 1->2->4, 1-&g

leetCode 21. Merge Two Sorted Lists 合并链表

21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目大意:合并两个有序的链表 思路:通过比较两个链表的节点大小,采用尾插法建立链表. 代码如下: /**  * Definition for singly-lin

21. Merge Two Sorted Lists(js)

21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4题意

leedCode练题——21. Merge Two Sorted Lists(照搬大神做法)

1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4-&g

[Leetcode][Python]21: Merge Two Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 21: Merge Two Sorted Listshttps://oj.leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as a new list.The new list should be made by splicing together th

C# 写 LeetCode easy #21 Merge Two Sorted Lists

21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 代

leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

23. Merge k Sorted Lists - LeetCode

Question 23.?Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 Java实现: public ListNode mergeKLists(ListNode[] lists) { ListNode preHead = new ListNode(0); ListNode minNode = getMinNode(lists); ListNode tmpNode =