8.17 可以自己 A 出来
题目描述
力扣:https://leetcode-cn.com/problems/merge-two-sorted-lists/
剑指:https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/
解题思路
K神题解:https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/solution/
class Solution {public ListNode mergeTwoLists(ListNode l1, ListNode l2) {ListNode res = new ListNode(0); // 创建一个头节点ListNode tmp = res; // 维护一个指针指向头节点,操作此指针也就操作了新建的链表while(l1 != null && l2 != null) {if(l1.val <= l2.val) {tmp.next = l1;l1 = l1.next;}else {tmp.next = l2;l2 = l2.next;}tmp = tmp.next;}tmp.next = l1 == null ? l2 : l1;return res.next;}}
