LeetCode_Linked List_Merge Two Sorted Lists

21.Merge Two Sorted Lists


1. 问题描述:

合并两个有序链表,并返回一个新的有序链表。

2. 解决思路:

这道题很简单。可以用递归求解,也可以用非递归求解;注意:如果用非递归求解,发现新链表的头结点不确定,所以引入dummy节点。不多说之间上代码。

3. java代码:

/**
 * 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 ret = null;
        if(l1 == null)
            return l2;
        if(l2 == null)
            return l1;

        if(l1.val > l2.val){
            ret = l2;
            ret.next = mergeTwoLists(l2.next,l1);
        } else {
            ret = l1;
            ret.next = mergeTwoLists(l1.next,l2);
        }
        return ret;*/

        /* 除了dummy节点外还引入一个lastNode节点充当下一次合并时的头节点。在l1或者l2的某一个节点为空指针NULL时,退出while循环,并将非空链表的头部链接到lastNode->next中。*/

        ListNode dummy = new ListNode(0);
        ListNode lastNode = dummy;
        while((l1!=null) && (l2!=null)){
            if(l2.val > l1.val){
                lastNode.next = l1;
                l1 = l1.next;
            } else {
                lastNode.next = l2;
                l2 = l2.next;
            }
            lastNode = lastNode.next;
        }

        lastNode.next = (l1!=null)?l1:l2;
        return dummy.next;
    }
}

4. 算法评估:



希望大家多多指正交流!

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

时间: 2024-08-02 19:27:53

LeetCode_Linked List_Merge Two Sorted Lists的相关文章

【leetcode】Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 根据k个已经排好序的链表构造一个排序的链表,采用类似归并排序的算法可以通过测试 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

LeetCode_Merge Two Sorted Lists

一.题目 Merge Two Sorted Lists Total Accepted: 63974 Total Submissions: 196044My Submissions 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. Show Tags Have you

LeetCodeMerge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 对每一个排序链表都设置一个指针.每次讲指针指向的单元中最小值链接,直到所有指针为空. public class Solution {    public ListNode mergeKLists(List<ListNode> lists) {                 int length =

Merge Two Sorted Lists &amp; Remove Nth Node From End of List

1.合并两个排好序的list 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.删除list倒数第n个元素 Remove Nth Node From End of List Given a linked list,

[LeetCode]148.Merge Two Sorted Lists

[题目] Sort a linked list in O(n log n) time using constant space complexity. [分析] 单链表适合用归并排序,双向链表适合用快速排序.本题可以复用Merge Two Sorted Lists方法 [代码] /********************************* * 日期:2015-01-12 * 作者:SJF0115 * 题目: 148.Merge Two Sorted Lists * 来源:https://

[Leetcode][JAVA] Merge Two Sorted Lists &amp; Sort List

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. 两个排好序的链表拼接,只要用两个指针遍历链表即可. 可以借助一个helper指针来进行开头的合并.如果有一个遍历完,则直接把另一个链表指针之后的部分全部接在后面: 1

Leetcode 23.Merge Two Sorted Lists Merge K 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. 依次拼接 复杂度 时间 O(N) 空间 O(1) 思路 该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后.最后