Merge Two Sorted Lists

原创 2015年11月19日 12:50:37

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.

用了一个dumb head,。对于这种header不确定的list,可以用dumb header,代码会整洁不少。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode dumHeader = new ListNode(0);
        ListNode current = dumHeader;
        while(l1 != null && l2 != null){
            ListNode nextNode = null;
            if(l1.val > l2.val){
                nextNode = l2;
                l2 = l2.next;
                current.next = nextNode;
                current = nextNode;
            }
            else if(l2.val >= l1.val){
                nextNode = l1;
                l1 = l1.next;
                current.next = nextNode;
                current = nextNode;
            }
        }
        
        if(l1 != null){
            current.next = l1;
        }
        else if (l2 != null){
            current.next = l2;
        }
        
        return dumHeader.next;
    }
}


C++的背影——C++之父Bjarne Stroustrup印象

C++的背影——C++之父Bjarne Stroustrup印象左轻侯2002.11.4  热爱C++的朋友请不要误会,我并不是在暗示“C++已经日薄西山”,或者任何类似的意思。从语义上来说,C++作...
  • qinghou
  • qinghou
  • 2003-01-06 10:41:00
  • 4199

Merge Two Sorted Lists -- LeetCode

原题链接: http://oj.leetcode.com/problems/merge-two-sorted-lists/  这道题目比较简单,经典的链表基本操作。维护两个指针对应两个链表,因为一般会...
  • linhuanmars
  • linhuanmars
  • 2014-02-22 23:31:00
  • 16923

21. Merge Two Sorted Lists(python)

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t...
  • qq_28119401
  • qq_28119401
  • 2016-09-18 21:32:17
  • 770

[LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)

[LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)
  • hcbbt
  • hcbbt
  • 2015-03-04 20:11:32
  • 3765

[leetcode-排序]--21. Merge Two Sorted Lists

Question 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The...
  • u010853261
  • u010853261
  • 2017-02-05 16:53:38
  • 915

Leetcode 21. Merge Two Sorted Lists(C++)

问题: Merge Two Sorted Lists (将两个有序单链表合成一个有序的单链表) 参考链接: 1、[LeetCode]21.Merge Two Sorted Lists...
  • sqiu_11
  • sqiu_11
  • 2017-04-24 15:02:29
  • 550

【leetcode c++】21 Merge Two Sorted Lists

Merge two sorted linked lists and return itas a new list. The new list should be made by splicing to...
  • hqq39
  • hqq39
  • 2015-06-18 13:37:28
  • 491

[C语言][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 sh...
  • Timsley
  • Timsley
  • 2016-03-04 08:26:24
  • 938

LeetCode | Merge Two Sorted Lists

题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici...
  • lanxu_yy
  • lanxu_yy
  • 2013-09-14 19:11:56
  • 6681

LeetCode 21:Merge Two Sorted Lists

LeetCode 21: Merge Two Sorted Lists   Merge two sorted linked lists and return it as a new list. ...
  • sunao2002002
  • sunao2002002
  • 2015-05-27 23:08:48
  • 4123
收藏助手
不良信息举报
您举报文章:Merge Two Sorted Lists
举报原因:
原因补充:

(最多只允许输入30个字)