描述
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
难度
Easy
题目链接:
https://leetcode.com/problems/merge-two-sorted-lists/
思路
合并两个有序链表,重点在 while 循环中,前面增加了两个判断,主要是因为两个链表一起遍历,可能其中一个已经遍历完毕了,此时将另一个链表拼到结果链表后面即可。
正常的逻辑是 if 语句,主要是判断当前两个结点的大小,然后决定哪个链表向后移动一位。
public static void main(String...args) {
ListNode l1 = new ListNode(1);
ListNode l12 = new ListNode(2);
ListNode l13 = new