算法第五周Merge Two Sorted Lists[easy]
来源:互联网 发布:linux解压tar.xz文件 编辑:程序博客网 时间:2024/05/24 16:15
Merge Two Sorted Lists[easy]
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.
Solution
这道题目是将两个已经排序的链表排序得到新的链表。
我们只需遍历两个链表不断地将值小的结点链接到新链表上即可。
特别要注意指针为空的情况。
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* head = new ListNode(INT_MIN); ListNode* t = head; while (l1 && l2) { if (l1->val < l2->val) { t->next = l1; t = t->next; l1 = l1->next; } else { t->next = l2; t = t->next; l2 = l2->next; } } if (l1 == NULL&&l2 != NULL) { t->next = l2; } if (l1 != NULL&&l2 == NULL) { t->next = l1; } return head->next; }};
阅读全文
0 0
- 算法第五周Merge Two Sorted Lists[easy]
- Merge Two Sorted Lists (EASY)
- (算法分析Week3)Merge Two Sorted Lists[Easy]
- [Leetcode 21, Easy] Merge Two Sorted Lists
- 21. Merge Two Sorted Lists [easy] (Python)
- Merge Two Sorted Lists(easy)
- 21. Merge Two Sorted Lists (Easy)
- 【Leetcode-Easy-21】Merge Two Sorted Lists
- LeetCode 21. Merge Two Sorted Lists (Easy)
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- 【java基础】17.10.7:static什么时候使用?有关静态变量(类变量)和静态方法(类方法)的解析
- 1.实现一个函数,可以左旋字符串中的k个字符。 AABCD左旋一个字符得到ABCDA AABCD左旋两个字符得到BCDAA 2.判断一个字符串是否为另外一个字符串旋转之后的字符串。 例如:给定s1
- 数字图像处理(一)
- Log4j 2架构
- Opencv3.2.0 SVM简单尝试
- 算法第五周Merge Two Sorted Lists[easy]
- HDU 6006 Engineer Assignment(状态压缩dp)
- codeforces 869E The Untended Antiquity 二维BIT + hash
- 二叉树基本概念一览
- C语言 打印图形
- asio post讲解
- c语言 求1000到2000之间的闰年
- 遗传算法简单介绍与MATLAB实现(二)
- HDFS命令