目录
人各有所感,角度不同又怎能感同身受
—— 24.9.23
21. 合并两个有序链表
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例 1:
输入:l1 = [1,2,4], l2 = [1,3,4] 输出:[1,1,2,3,4,4]示例 2:
输入:l1 = [], l2 = [] 输出:[]示例 3:
输入:l1 = [], l2 = [0] 输出:[0]提示:
- 两个链表的节点数目范围是
[0, 50]
-100 <= Node.val <= 100
l1
和l2
均按 非递减顺序 排列
方法1 递归
思路
递归函数应该返回更小的那个链表节点,并把它剩余节点与另一个链表再次递归;返回之前,更新此节点的 next
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
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;
}
}
}
方法2 迭代
思路
新建一个链表,以指针p为头结点,原来的两链表头结点分别为p1和p2,比较原来的两链表指针指向的数据,谁小,把谁链接给新链表,指针p和小的结点所在的原链表的指针都后移一位
当原先两链表指针p1、p2有一个为null时,退出循环,把不为null的链表的剩余节点链接给新链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1);
ListNode prev = prehead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}
// 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
prev.next = l1 == null ? l2 : l1;
return prehead.next;
}
}
完整代码
结点类
public class ListNode {
public int val;
public ListNode next;
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
public static ListNode of(int...numbers) {
ListNode head = new ListNode(0, null);
ListNode s = new ListNode(-1,head);
ListNode current = head;
for (int number : numbers) {
current.next = new ListNode(number, null);
current = current.next;
}
return head;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(64);
sb.append("[");
ListNode p = this.next.next;
while (p != null) {
sb.append(p.val);
if (p.next != null) {
sb.append(",");
}
p = p.next;
}
sb.append("]");
return sb.toString();
}
}
方法
public class LeetCode21MergeTwoLists {
public ListNode mergeTwoLists1(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if(l1.val <= l2.val){
l1.next = mergeTwoLists1(l1.next, l2);
return l1;
}else{
l2.next = mergeTwoLists1(l1, l2.next);
return l2;
}
}
public ListNode mergeTwoLists2(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1,null);
ListNode prev = prehead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}
// 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
prev.next = l1 == null ? l2 : l1;
return prehead.next;
}
public static void main(String[] args) {
ListNode L1 = ListNode.of(1,3,8,9);
ListNode L2 = ListNode.of(2,4,6,8);
ListNode L3 = new LeetCode21MergeTwoLists().mergeTwoLists2(L1, L2);
ListNode L4 = new LeetCode21MergeTwoLists().mergeTwoLists1(L1, L2);
System.out.println(L4.toString());
System.out.println(L3.toString());
}
}
标签:12,ListNode,val,next,链表,l2,l1,习题
From: https://blog.csdn.net/m0_73983707/article/details/142455113