Leet Code 21 合并两个有序链表 - Java
2016-06-08 00:00
459 查看
摘要: Leet Code 21 Merge Two Sorted Lists - 合并两个有序链表 - Java
问题原始链接 https://leetcode.com/problems/merge-two-sorted-lists
合并两个有序链表,返回新的链表,新链表通过连接原链表的节点组成。
问题原始链接 https://leetcode.com/problems/merge-two-sorted-lists
合并两个有序链表,返回新的链表,新链表通过连接原链表的节点组成。
[code=plain]/** * 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) { ListNode dummyHead = new ListNode(0); ListNode p = dummyHead; while (l1 != null && l2 != null) { if (l1.val <= l2.val) { p.next = l1; l1 = l1.next; } else { p.next = l2; l2 = l2.next; } p = p.next; } p.next = l1 != null ? l1 : l2; return dummyHead.next; } }
相关文章推荐
- 探讨:将两个链表非降序合并为一个链表并依然有序的实现方法
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解