[leetcode] 17. 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.
就是算导里面的第一个算法讲解,但是我的C++水平实在太渣,所以对于链表的操作很蠢,但还好完成了。题解如下:
/**
* 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)
{
if (l1 == NULL && l2 == NULL)
{
return NULL;
} if (l1 == NULL || l2 == NULL)
{
return (l1 == NULL) ? (l2) : (l1);
}
ListNode *aspros, *deferos, *root;
aspros = deferos = new ListNode(0);
if (l1->val <= l2->val)
{
aspros->val = l1->val;
aspros->next = NULL;
l1 = l1->next;
}
else
{
aspros->val = l2->val;
aspros->next = NULL;
l2 = l2->next;
}
root = aspros; while (l1 && l2)
{
if (l1->val <= l2->val)
{
aspros = new ListNode(0);
aspros->val = l1->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l1 = l1->next;
}
else
{
aspros = new ListNode(0);
aspros->val = l2->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l2 = l2->next;
}
} while (l1)
{
aspros = new ListNode(0);
aspros->val = l1->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l1 = l1->next;
} while (l2)
{
aspros = new ListNode(0);
aspros->val = l2->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l2 = l2->next;
} return root;
}
};
这个就是因为我不会初始化链表,所以在一开始的地方非常蠢,作了两个判断,一个是l1与l2都为NULL的情况,另一个是它俩有一个是空的情况,可以直接弹出,接着就是合并了。
[leetcode] 17. Merge Two Sorted Lists的更多相关文章
- Java for LeetCode 023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- [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 t ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- [Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...
- [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 t ...
- [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 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 shoul ...
随机推荐
- Anaconda中python加入环境变量
1.我的电脑---高级系统设置 2.选中环境变量,保存. 3.在系统环境变量PATH中,加入Anaconda3及Script路径加入其中 4.测试python
- New Document (2)
#Markdown 语法说明 (简体中文版) / (点击查看快速入门) ##概述 ###宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 ...
- springboot取得resources下的文件
参考http://blog.csdn.net/programmeryu/article/details/58002218 ResourceUtils.getFile("classpath:p ...
- IsPostBack用法
可以自己定义 在页面中定义隐藏的input,设置为ispostback. <form action="" method=""> <input ...
- 第五章 二叉树(d)二叉树实现
- 第七章 二叉搜索树(b3)BST:删除
- Spring AOP开发
--------------------siwuxie095 Spring AOP 开发 1.在 Spring 中进行 ...
- 二叉树的层次遍历 · Binary Tree Level Order Traversal
[抄题]: 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) [思维问题]: [一句话思路]: 用queue存每一层 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况 ...
- handler通信机制
package com.example.gp08_day26_handler3; import android.os.Bundle; import android.os.Handler; import ...
- discuz回贴通知插件实现-配置邮件服务器
添加smtp服务器,填写相应的smtp服务器,发信人地址,用户名和密码. 填写发件人地址和收件人地址来测试邮件是否发送成功.