21. Merge Two Sorted Lists

原创 2016年06月01日 18:34:30

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-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 p1 = l1;
       ListNode p2 = l2;
       ListNode presult = new ListNode(0);
       ListNode p = presult;
       
       while(p1 != null && p2 != null){
           if(p1.val >= p2.val){
               p.next = p2;
               p2 = p2.next;
           }else{
               p.next = p1;
               p1 = p1.next;
           }
           p = p.next;
       }
       if(p1 != null){
           p.next = p1;
       }
       if(p2 != null){
           p.next = p2;
       }
       
       return presult.next;
      }
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

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
  • 627

LeetCode 21 Merge Two Sorted Lists(合并两个已排序的链表)(Linked List)

翻译合并两个排好序的链表,并返回这个新链表。 新链表应该由这两个链表的头部拼接而成。原文Merge two sorted linked lists and return it as a new lis...
  • NoMasp
  • NoMasp
  • 2015年11月10日 20:38
  • 8159

[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
  • 3598

【leetcode】【21】Merge Two Sorted Lists

一、问题描述 Merge two sorted linked lists and return it as a new list. The new list should be made by sp...
  • xiaoliucool1314
  • xiaoliucool1314
  • 2016年02月28日 11:23
  • 235

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
  • 4031

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

[leetcode 21 ] Merge Two Sorted Lists c语言实现
  • banana1006034246
  • banana1006034246
  • 2017年03月29日 22:24
  • 246

Merge Two Sorted Lists -- LeetCode

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

leetcode 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 t...
  • wangyaninglm
  • wangyaninglm
  • 2015年06月05日 21:59
  • 2088

leetcode第21题——*Merge Two Sorted Lists

题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin...
  • buptlrw
  • buptlrw
  • 2016年02月28日 10:09
  • 546

[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
  • 835
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:21. Merge Two Sorted Lists
举报原因:
原因补充:

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