LeetCode 21

已经已连续做了好几道题了,感觉停不下来了。突然来了兴趣了,这个题让我认为思路非常清晰,合并两个排序的链表。

我的思路例如以下:分别从两个链表里面摘取节点放到新的链表中。最后摘到一个也不留即可了。只是须要注意的是一些小细节。基本的注意事项例如以下:

1.两空(两个空的链表)

2.一空(一个链表为空一个部位空)

3.一个摘完了,另外的可能还有节点(这个须要好好注意一下,非常easy出错的)

好了,见代码吧:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
    {
        //相似于从两个链表上取下节点,放到newHead中去
        if(l1 == NULL && l2 == NULL)
        {
            return NULL;
        }
        if(l1 == NULL)
        {
            return l2;
        }
        if(l2 == NULL)
        {
            return l1;
        }

        ListNode * newHead=NULL;
        if(l1->val <= l2->val)
        {
            newHead=l1;
            l1=l1->next;
        }
        else
        {
             newHead=l2;
             l2=l2->next;
        }
        newHead->next = NULL;
        ListNode *cur = newHead;
        while(l1 && l2)
        {
            if(l1->val <= l2->val)
            {
                cur->next = l1;
                l1=l1->next;
            }
            else
            {
                cur->next = l2;
                l2 = l2->next;
            }
            cur=cur->next;
            cur->next=NULL;
        }

        if(l1)
        {//说明 l2 完了。仅仅剩 l1 了
            cur->next = l1;
        }
        if(l2)
        {//说明 l1 完了,仅仅剩 l2 了
            cur->next=l2;
        }

        return newHead;
    }
};

结果例如以下:

时间: 2024-11-04 22:12:55

LeetCode 21的相关文章

LeetCode 21 23:Merge Two Sorted Lists &amp; Merge K 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. 题目分析:对两个有序列表进行合并,这个是数据结构基本题目比较简单,代码如下(这个代码有点长,可以简化): ListNode *mergeTwoL

[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. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode21. Merge Two Sorted Lists 示例: 输入: 1->2->4, 1->3->4 输出: 1->1->2->3->4->4 Java 实现 ListNode 类 class ListNode { int val; ListNode n

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

题目要求是,给你两个sorted LinkedList,然后把它们两个合并成一个新的LinkedList.思路很简单,就是比较ListNode L1 和 L2,哪个小就把那个node放到新的list里,并且移动相应ListNode的指针(L1或L2).注意其中一个为null,另外一个不为null的时候别漏掉就好. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val;

[leetcode] 21. 合并两个有序链表

21. 合并两个有序链表 两个有序链表合并为一个新的有序链表 class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode ans = new ListNode(Integer.MAX_VALUE); ListNode p = ans; while (l1 != null && l2 != null) { if (l1.val < l2.val) { p.next = l1; l

19.1.29 [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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 1 class Solution { 2 public:

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. * struct ListNode

[LeetCode]21. 3Sum三者之和

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solut

LeetCode #21 Merge Two Sorted Lists (E)

[Problem] 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] 没什么好说的,就是按序visit并把两个list的node接在一起. [Solution] public class Solution { public ListNode m