Easy 21题 Merge Two Sorted Lists(必须重刷!!!)
来源:互联网 发布:贵人 知乎 编辑:程序博客网 时间:2024/05/05 11:45
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.
SOLUTION
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { /* if (l1 == null) return l2; if (l2 == null) return l1; //we always get the small one if (l1.val > l2.val) { ListNode tmp = l2; tmp.next = mergeTwoLists(l1, l2.next); return tmp; } else { ListNode tmp = l1; tmp.next = mergeTwoLists(l1.next, l2); return tmp; } */ if (l1 == null || l2 == null) { return l1 == null ? l2 : l1; } ListNode tmp=null; ListNode head=null; if(l1.val>l2.val) { head=l2; l2=l2.next; } else { head=l1; l1=l1.next; } tmp=head; while(l1!=null&&l2!=null) { if(l1.val>l2.val) { tmp.next=l2; l2=l2.next; } else { tmp.next=l1; l1=l1.next; } tmp=tmp.next; System.out.print("123"); } if(l1==null) { tmp.next=l2; } else if(l2==null) { tmp.next=l1; } return head; }}
0 0
- Easy 21题 Merge Two Sorted Lists(必须重刷!!!)
- [Leetcode 21, Easy] Merge Two Sorted Lists
- 【Leetcode-Easy-21】Merge Two Sorted Lists
- Merge Two Sorted Lists(easy)
- LeetCode-21-Merge Two Sorted Lists(链表/归并)-Easy
- Easy 6 Merge Two Sorted Lists(21)
- Merge Two Sorted Lists (EASY)
- LeetCode OJ 21 Merge Two Sorted Lists [Easy]
- 21. Merge Two Sorted Lists [easy] (Python)
- 21. Merge Two Sorted Lists (Easy)
- LeetCode 21. Merge Two Sorted Lists (Easy)
- 第21题:Merge Two Sorted Lists
- LeetCode(21)Merge Two Sorted Lists
- LeetCode (21)Merge Two Sorted Lists
- 21、Merge Two Sorted Lists
- 21Merge Two Sorted Lists
- #21 Merge Two Sorted Lists
- #21 Merge Two Sorted Lists
- 洛谷 U5122 T2-power of 2(费马小定理)
- 纪中2016.10.6比赛不明总结
- 我的互联网小屋
- 安装wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12
- 求2个整数的正差值
- Easy 21题 Merge Two Sorted Lists(必须重刷!!!)
- 勾股定理一日一证连载125
- 以太网通信,UDP通信
- 2016.10.06【初中部 NOIP提高组 】模拟赛C
- 【USACO题库】2.1.2 Ordered Fractions顺序的分数.tj
- 总结 2016.10.6
- 2016.10.6总结
- Pond
- 石子合并问题