227 字
1 分钟
21. Merge Two Sorted Lists
2021-09-04
2024-03-26

Problem#

https://leetcode-cn.com/problems/merge-two-sorted-lists/

Solutions#

/**
* 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 list1, ListNode list2) {
ListNode newList = new ListNode();
ListNode curr = newList;
while(list1 != null && list2 != null) {
if(list1.val <= list2.val) {
curr.next = list1;
list1 = list1.next;
} else {
curr.next = list2;
list2 = list2.next;
}
curr = curr.next;
}
if(list1 == null) {
curr.next = list2;
}
if(list2 == null) {
curr.next = list1;
}
return newList.next;
}
}
  1. 输入两个有序链表
  2. 循环遍历直到两个链表均指向null
  3. 比较list1和list2的当前值,把curr指针指向小的那一个list
  4. list和curr指针均向后移动
  5. 循环直到其中一个list为null
  6. 由于另一个非null的list还有元素,所以需要把它附加到curr上,同时解决输入其中一个list直接为空的情况(list1为空,则curr.next = list2)

Reference#

N/A

21. Merge Two Sorted Lists
https://static.next.liqimore.com/2021/leetcode-merge-two-sorted-lists
作者
Ricky Li
发布于
2021-09-04
许可协议
CC BY-NC-SA 4.0
如果评论不显示,请刷新页面重新加载. Please refresh if comments didn't show up.