【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.

思路:比较每个列表的第一个元素。 合并小的添加到列表中。 最后,当其中一个是空的,只需将它附加到合并后的列表,因为它已经排序。

/**
 * 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 head=new ListNode(-1);
        ListNode addNode=head;
        while(l1!=null&&l2!=null){
            if(l1.val<l2.val){
                addNode.next=l1;
                l1=l1.next;
            }else{
                addNode.next=l2;
                l2=l2.next;
            }
            addNode=addNode.next;
        }
        if(l1==null) addNode.next=l2;
        if(l2==null) addNode.next=l1;
        return head.next;
    }
}
时间: 2024-09-30 10:20:56

【LeetCode】21. Merge Two Sorted Lists的相关文章

【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. 解题分析: 再基础不过的题了,直接看代码吧^-^ 具体代码: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode

【LeetCode】21. Merge Two Sorted Lists合并两个有序链表,得到新的有序链表

一.描述: 二.思路: 两个有着相同排序类型(降序或升序)的链表,合并为一新的链表,并且要求有序(和两个子链表的排序相同): 判断2个子链表是否为空,若有空链表,则返回另一不为空的链表: 两者均不为空,判断链表结点值val的大小,(此处应该有2中排序结果,大->小 或 小->大),该题中提交只接受 小->大 类型: 故新的链表头指针指向val值较小的结点,子链表中去除已经被新链表指向的结点,再次重复该操作,即新链表头指针.next指向下一个较小值对应的结点--,即递归实现. 三.代码:

【LeetCode】23. Merge k Sorted Lists

合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* mergeKLists(vect

[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】#23. Merge k Sorted Lists

一天一道LeetCode系列 (一)题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. (二)解题 合并K个已拍好序的链表.剑指上有合并两个已排好序的链表的算法,那么K个数,我们可以采用归并排序的思想,不过合并函数可能需要修改一下,换成合并两个已排好序的链表的方法.代码如下: /** * Definition for singly-linked

[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

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22