From bc221fed22a250d2dee98d4c770c601b2877d26e Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Mon, 1 Jun 2020 12:39:53 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E9=93=BE=E8=A1=A8=E7=9B=B8=E5=85=B3.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blogs/Algorithm/剑指 Offer/链表相关.md | 47 ++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/链表相关.md b/blogs/Algorithm/剑指 Offer/链表相关.md index 5b6c521..034edb0 100644 --- a/blogs/Algorithm/剑指 Offer/链表相关.md +++ b/blogs/Algorithm/剑指 Offer/链表相关.md @@ -122,3 +122,50 @@ class Solution { } ``` +[25. 合并两个排序的链表](https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/) + +```java +class Solution { + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode h0 = new ListNode(0); + ListNode h = h0; + while (l1 != null && l2 != null) { + if (l1.val <= l2.val) { + h0.next = l1; + l1 = l1.next; + } else { + h0.next = l2; + l2 = l2.next; + } + h0 = h0.next; + } + if (l1 == null) { + h0.next = l2; + } else { + h0.next = l1; + } + return h.next; + } +} +``` + +```java +class Solution { + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + if (l1 == null) { + return l2; + } + if (l2 == null) { + return l1; + } + if (l1.val <= l2.val) { + l1.next = mergeTwoLists(l1.next, l2); + return l1; + } else { + l2.next = mergeTwoLists(l1, l2.next); + return l2; + } + } +} +``` +