一、題目名稱java
Merge Two Sorted Lists(按升序拼接兩個有序鏈表)node
二、題目地址code
https://leetcode.com/problems/merge-two-sorted-lists/ci
三、題目內容leetcode
英文: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.開發
中文:給定兩個已經排好序(升序)的鏈表,將它們拼接成一個新的有序(升序)鏈表get
四、解題方法it
本題的解決方法比較比較簡單明瞭,Java代碼以下:io
/** * @功能說明:LeetCode 88 - Merge Two Sorted Lists * @開發人員:Tsybius2014 * @開發時間:2015年11月16日 */ public class Solution { /** * 按升序拼接兩個有序鏈表 * @param l1 鏈表1 * @param l2 鏈表2 * @return */ public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode start = new ListNode(0); ListNode result = start; while (l1 != null || l2 != null) { if (l1 == null || (l1 != null && l2 != null && l1.val >= l2.val)) { result.next = new ListNode(l2.val); result = result.next; l2 = l2.next; } else { result.next = new ListNode(l1.val); result = result.next; l1 = l1.next; } } return start.next; } }
ENDclass