【leetcode】 21. Merge Two Sorted Lists
2016-07-05 00:29
429 查看
题目描述:
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.
解题分析:
再基础不过的题了,直接看代码吧^-^
具体代码:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public static ListNode mergeTwoLists(ListNode head1, ListNode head2) { if(head1==null) return head2; if(head2==null) return head1; ListNode head=null; ListNode current=null; if(head1.val<=head2.val){ head=head1; head1=head1.next; head.next=null; } else{ head=head2; head2=head2.next; head.next=null; } current=head; while(head1!=null&&head2!=null){ if(head1.val<=head2.val){ current.next=head1; current=current.next; head1=head1.next; current.next=null; } else{ current.next=head2; current=current.next; head2=head2.next; current.next=null; } } if(head1!=null){ current.next=head1; } if(head2!=null){ current.next=head2; } return head; } }
相关文章推荐
- LeetCode 21. Merge Two Sorted Lists
- leetcode21. Merge Two Sorted Lists
- [LeetCode]21. Merge Two Sorted Lists
- LeetCode-Easy部分中标签为LinkedList 21. Merge Two Sorted Lists
- [leetcode] 21. Merge Two Sorted Lists 解题报告
- LeetCode 21. Merge Two Sorted Lists
- LeetCode 21. Merge Two Sorted Lists
- 21. Merge Two Sorted Lists leetcode(lists)
- Leetcode题解 21. Merge Two Sorted Lists
- 2018.1.8 LeetCode 21. Merge Two Sorted Lists 【链表的基本运用】
- LeetCode_21. Merge Two Sorted Lists
- leetcode 21. Merge Two Sorted Lists
- 21. Merge Two Sorted Lists-leetcode-java
- LeetCode21. Merge Two Sorted Lists
- leetcode_效率题解_[python/C++]_21. Merge Two Sorted Lists(合并2个有序链表)
- LeetCode: 21. Merge Two Sorted Lists
- 【LeetCode】21. Merge Two Sorted Lists
- [LeetCode] 21. Merge Two Sorted Lists 解题思路
- LeetCode21. Merge Two Sorted Lists
- LeetCode 21. Merge Two Sorted Lists