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-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
   public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if(l1==null)
        return l2;
    if(l2==null)
        return l1;
    ListNode headNode=new ListNode(0);
    ListNode head=headNode;
    head.next=null;
    ListNode r=head,p=l1,q=l2;
    while(p!=null&&q!=null){
        if(p.val<=q.val){
            r.next=p;
            r=r.next;
            p=p.next;
        }
        else{
            r.next=q;
            r=r.next;
            q=q.next;
        }
    }
    if(p!=null)
        r.next=p;
    else
        r.next=q;
    return head.next;
}
}

  

时间: 2024-11-06 02:59:28

21.Merge Two Sorted Lists(链表)的相关文章

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

[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

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 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. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

[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 代

21. Merge Two Sorted Lists (Java 合并有序链表 空间复杂度O(1))

题目: 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 ListNode mergeTwoLists(Li

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 together the nodes of the first two lists. 翻译: 把2个有序链表连接,返回一个新链表 思路: 很简单,就是遍历每个节点,小的话添加在新链表的后面.如果一个为空,则把另一个接在新链表结尾. 第二种思路就是以一个链表为基准,然后和另一个比较,如果l2的节

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. 不申请新的节点(把两个链表的节点接起来) 提交通过: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode v(0),*pre = &v,*p