leetcode21
生活随笔
收集整理的這篇文章主要介紹了
leetcode21
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*** Definition for singly-linked list.* public class ListNode {* public int val;* public ListNode next;* public ListNode(int x) { val = x; }* }*/
public class Solution {public ListNode MergeTwoLists(ListNode l1, ListNode l2) {//遞歸實(shí)現(xiàn)鏈表合并ListNode l = null;if (l1 == null && l2 != null){return l2;}else if (l2 == null && l1 != null){return l1;}else if (l1 == null && l2 == null){return null;}else{if (l1.val < l2.val){if (l == null){l = l1;}l.next = MergeTwoLists(l1.next, l2);}else{if (l == null){l = l2;}l.next = MergeTwoLists(l1, l2.next);}return l;}}
}
https://leetcode.com/problems/merge-two-sorted-lists/#/description
?
補(bǔ)充一個(gè)python的實(shí)現(xiàn):
1 class Solution: 2 def mergeTwoLists(self, l1: 'ListNode', l2: 'ListNode') -> 'ListNode': 3 if l1 == None: 4 return l2 5 if l2 == None: 6 return l1 7 if l1.val < l2.val: 8 l1.next = self.mergeTwoLists(l1.next,l2) 9 return l1 10 else: 11 l2.next = self.mergeTwoLists(l2.next,l1) 12 return l2?
轉(zhuǎn)載于:https://www.cnblogs.com/asenyang/p/6737731.html
總結(jié)
以上是生活随笔為你收集整理的leetcode21的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [20170420]表达式加0或者减0不
- 下一篇: [剑指offer]面试题第[47]题[J