leetcode: Merge Two Sorted Lists
Problem
Merge Two Sorted Lists[^1], 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.
分析
基础题,相加两链表代表的数字,两链表已经逆序,所以直接从head加起即可,用carry记录当前是否有进位,注意:
- 判断一个已经到达尾部情况
- 两链表加完,仍有carry情况
- list问题很多用pseudoHead能简化逻辑
Solution
1 | func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { |
[^1]: Merge Two Sorted Lists