leetcode21:Merge Two Sorted Lists
来源:互联网 发布:淘宝梦幻西游7折祥瑞 编辑:程序博客网 时间:2024/05/16 17:49
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.
两个单链表的合并
package leetcode;public class leet21 {private static class ListNode{int val;ListNode next;ListNode(){}ListNode(int x){ val = x;}}public static void main(String[] args) {ListNode l0 = new leet21.ListNode();ListNode l1 = new leet21.ListNode(1);ListNode l2 = new leet21.ListNode(2);ListNode l3 = new leet21.ListNode(5);l0.next = l1;l1.next = l2;l2.next = l3;ListNode h0 = new leet21.ListNode();ListNode h1 = new leet21.ListNode(3);ListNode h2 = new leet21.ListNode(4);ListNode h3 = new leet21.ListNode(6);h0.next = h1;h1.next = h2;h2.next = h3;leet21 leet = new leet21();ListNode node = leet.mergeTwoList(l0, h0);while(node != null){System.out.println(node.val);node = node.next;}}public ListNode mergeTwoList(ListNode l1,ListNode l2){ListNode head = l1;ListNode p = l1;l1 = l1.next;l2 = l2.next;while(l1 != null && l2 !=null){if(l1.val < l2.val){p.next = l1;p = l1;l1 = l1.next;}else{p.next = l2;p = l2;l2 = l2.next;}}if(l1 == null)p.next = l2;if(l2 == null)p.next = l1;return head;}}
0 0
- [LeetCode21]Merge Two Sorted Lists
- leetcode21 Merge Two Sorted Lists
- LeetCode21:Merge Two Sorted Lists
- leetcode21. Merge Two Sorted Lists
- LeetCode21. Merge Two Sorted Lists
- Leetcode21: Merge Two Sorted Lists
- leetcode21:Merge Two Sorted Lists
- leetcode21~Merge Two Sorted Lists
- leetcode21. Merge Two Sorted Lists
- LeetCode21. Merge Two Sorted Lists
- LeetCode21. Merge Two Sorted Lists
- LeetCode21:Merge Two Sorted Lists
- leetcode21. Merge Two Sorted Lists
- LeetCode21. Merge Two Sorted Lists
- Leetcode21. Merge Two Sorted Lists
- LeetCode21——Merge Two Sorted Lists
- 【LeetCode21】【Merge Two Sorted Lists】递归实现
- Merge Two Sorted Lists
- bzoj 2940: [Poi2000]条纹 (Multi-SG游戏)
- 基于GBT28181:SIP协议组件开发-----------第一篇环境搭建
- Button 增加图标 Menu
- EditText隐藏输入弹出框?
- 简易五子棋 c语言
- leetcode21:Merge Two Sorted Lists
- RxJava+Retrofit+OkHttp深入浅出-终极封装六特殊篇(变种String替换Gson自由扩展)
- Android UI:使用矢量图,抛弃PNG
- 骑士旅行
- JavaEE – JPA(1):事务的基础概念
- java数组学习之二:使用二维数组输出杨辉三角
- RxJava+Retrofit+OkHttp深入浅出-终极封装七(异常处理)
- opencv,我的电脑转换2.49版本和3.0版本笔记(vs2015)
- Java的Class类提供了许多的getDeclaredxxx和getxxx方法,例如getDeclaredMethod和getMethod成对出现,那它们有什么区别呢?