LeetCode -- Merge Two Sorted Linked List

Question:

  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.

Analysis:

  1)两个都是空串;

  2)一个是空串;

  3)新建一个伪头结点,用于新串的连接;

  4)最后还要检查是否有一个串还有数字。

Answer:

public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode p1 = l1;
        ListNode p2 = l2;

        if(l1 == null && l2 == null)
            return null;
        if(l1 == null && l2 != null)
            return l2;
        if(l1 != null && l2 == null)
            return l1;

        ListNode fakeHead = new ListNode(0);
        ListNode p = fakeHead;

        while (p1 != null && p2 != null){
            if (p1.val <= p2.val) {
                p.next = p1;
                p1 = p1.next;
                p = p.next;
            } else {
                p.next = p2;
                p2 = p2.next;
                p = p.next;
            }
        }

        if(p1 != null)
            p.next = p1;
        if(p2 != null)
            p.next = p2;
        return fakeHead.next;

      }
时间: 2024-08-25 04:48:31

LeetCode -- Merge Two Sorted Linked List的相关文章

[leetcode] Merge k Sorted list

题目:(DC,LinkedList,Heap) Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解: leetcode中不多的heap题,priorityqueue来表示heap,注意在构造new priorityqueue的时候的方法. /** * Definition for singly-linked list. * public clas

[leetcode]Merge k Sorted Lists @ Python [基础知识: heap]

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路: 归并k个已经排好序的链表, 使用堆这一数据结构. 堆,也叫做:priority queue 首先将每条链表的头节点进入堆中. 然后将最小的弹出,并将最小的节点这条链

[leetcode]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归

LeetCode: Merge k Sorted Lists [022]

[题目] 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; * ListNode(int x) : val(x), n

LeetCode -- Merge k Sorted Lists (Divide and Conquer / PriorityQueue)

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) { //处理特殊情况  if (lists == null)             ret

LeetCode——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. 原题链接:https://oj.leetcode.com/problems/merge-two-sorted-lists/ 题目:归并两个有序链表,返回一个新的有序链表. 思路:同归并数组的解法一样,依次比較首位元素

Leetcode: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. * struct ListNode { * int val; * ListNode *next; * ListNode(int x

[leetcode]Merge Two Sorted Lists @ Python

原题地址:https://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 the nodes of the first two lists. 解题思路:合并两个已经排好序的链表. 代码: # Definition for sin

leetcode - 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. * struct ListNode { * int val; * ListNode *next; * ListNode(int x)