From e6e8711bd36e78d7f8c69ee27cbfbd5c60003d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Tue, 26 Jul 2022 16:20:12 +0800 Subject: [PATCH] =?UTF-8?q?BM4=20=E5=90=88=E5=B9=B6=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E7=9A=84=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../huangge1199/nowcoder/bm/BM4/Solution.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 bm/src/main/java/com/huangge1199/nowcoder/bm/BM4/Solution.java diff --git a/bm/src/main/java/com/huangge1199/nowcoder/bm/BM4/Solution.java b/bm/src/main/java/com/huangge1199/nowcoder/bm/BM4/Solution.java new file mode 100644 index 0000000..b9cac9b --- /dev/null +++ b/bm/src/main/java/com/huangge1199/nowcoder/bm/BM4/Solution.java @@ -0,0 +1,33 @@ +package com.huangge1199.nowcoder.bm.BM4; + +import com.huangge1199.nowcoder.common.ListNode; + +/** + * @author hyy + * @Classname Solution + * @Description BM4 合并两个排序的链表 + * @Date 2022/7/26 16:19 + */ +public class Solution { + public ListNode Merge(ListNode list1, ListNode list2) { + ListNode list = new ListNode(0); + ListNode head = list; + while (list1 != null && list2 != null) { + if (list1.val < list2.val) { + head.next = list1; + list1 = list1.next; + } else { + head.next = list2; + list2 = list2.next; + } + head = head.next; + } + if (list1 != null) { + head.next = list1; + } + if (list2 != null) { + head.next = list2; + } + return list.next; + } +}