LeetCode——Merge Two Sorted Lists
来源:互联网 发布:如何考上清华知乎 编辑:程序博客网 时间:2024/05/14 04:26
LeetCode——Merge Two Sorted Lists
#21
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.
这个问题是要将两个有序链表合并起来,这题就是对链表操作的考查。弄好结构关系,先做头指针,以免弄乱。主要思路就是将两个链表的val进行比较,将较小值的节点加入新链表,直到一个链表为空,就可以直接将另一个链表直接插入新链表。
这一题是挺简单的一题,我的解题方法如下,没啥创意,就笔直的写下去了。
- 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) { ListNode *h1 = new ListNode(0); ListNode *h2 = new ListNode(0); h1 -> next = l1; h2 -> next = l2; ListNode *h3 = new ListNode(0); ListNode *t = h3; if(h1 -> next == NULL) return l2; if(h2 -> next == NULL) return l1; while(h1 -> next != NULL || h2 -> next != NULL) { if(h1 -> next == NULL || (h2 -> next != NULL && h1 -> next -> val > h2 -> next -> val)) { t -> next = h2 -> next; t = t ->next; h2 -> next = t -> next; } else { t -> next = h1 -> next; t = t -> next; h1 -> next = t -> next; } } return h3 -> next; }};
这里我为了思路清晰,就写的比较详细,其实可以简化。少了几个头指针的操作,run time可以减少一点。但其实我觉得上面一种写法对理清链表的结构更有帮助。
/** * 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) { ListNode *l3 = new ListNode(0); ListNode *h3 = l3; if(l1 == NULL) return l2; if(l2 == NULL) return l1; while(l1 != NULL || l2 != NULL) { if(l1 == NULL || (l2 != NULL && l1 -> val > l2 -> val)) { h3 -> next = l2; l2 = l2 ->next; h3 = h3 -> next; } else { h3 -> next = l1; l1 = l1 -> next; h3 = h3 -> next; } } return l3 -> next; }};
- Java
* Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode l3 = new ListNode(0); ListNode h3 = dummy; while (l1 != null && l2 != null) { if (l1.val < l2.val) { h3.next = l1; l1 = l1.next; } else { lastNode.next = l2; l2 = l2.next; } h3 = h3.next; } if (l1 != null) { h3.next = l1; } else { h3.next = l2; } return l3.next; }}
Java的解法,run time在所有答案中还行,所以我没去修改了。可以在二刷的时候,做最优解。一刷我觉得主要是理清自己的思路,如果觉得这题的思路自己没什么大问题,运行不是特别慢,就不用太钻牛角尖,多刷一题也好。
- Python
# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ if l1 == None: return l2 if l2 == None: return l1 l3 = ListNode(0) h3 = l3 while l1 and l2: if l1.val <= l2.val: h3.next = l1 l1 = l1.next h3 = h3.next else: h3.next = l2 l2 = l2.next h3 = h3.next if l2 == None: h3.next = l1 else: h3.next = l2 return l3.next
LeetCode上用python写这题的人很少,没法看到比较时间,尴尬。
阅读全文
0 0
- LeetCode——Merge Two Sorted Lists
- LeetCode——Merge Two Sorted Lists
- LeetCode——Merge Two Sorted Lists
- LeetCode——Merge Two Sorted Lists
- LeetCode—Merge Two Sorted Lists
- leetcode——21——Merge Two Sorted Lists
- Python学习——leetcode(Merge Two Sorted Lists )
- leetcode 021 —— Merge Two Sorted Lists
- LeetCode题解——Merge Two Sorted Lists
- LeetCode OJ——Merge Two Sorted Lists
- leetcode刷题日记——Merge Two Sorted Lists
- leetcode第21题——*Merge Two Sorted Lists
- LeetCode 21. Merge Two Sorted Lists 题解 —— Java
- LeetCode算法题——21. Merge Two Sorted Lists
- Leetcode——21. Merge Two Sorted Lists
- LeetCode—Merge Two Sorted Lists融合两个有序单链表
- Leetcode Problem.21—Merge Two Sorted Lists
- LeetCode: Merge Two Sorted Lists
- mySql数据库出问题,整理一下mySql的安装与配置
- mybatis中的延迟加载
- hook read_chk 导致dex2oat进程 abort
- Spring fastjson format 无效
- POI解析文档内容(txt,doc,docx,xls,xlsx,ppt,pdf)
- LeetCode——Merge Two Sorted Lists
- 007 PyQuery
- Android 自定义布局对话框
- git创建仓库
- Java8函数式编程之三:函数式接口
- 用R进行文本挖掘与分析--软件分词统计词频
- 【模板】二叉搜索树
- git的传输协议及各自优缺点(本地传输协议,ssh,http,git)
- hessian原理分析(二)