LeetCode[24] 两两交换链表中的节点

“递归”: https://leetcode.com/tag/recursion/ “链表”: https://leetcode.com/tag/linked-list/ Similar Questions: “K 个一组翻转链表”: https://leetcode.com/problems/reverse-nodes-in-k-group/

Problem:

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

示例 1:

输入:head = [1,2,3,4]
输出:[2,1,4,3]

示例 2:

输入:head = []
输出:[]

示例 3:

输入:head = [1]
输出:[1]

提示:

  • 链表中节点的数目在范围 [0, 100]
  • 0 <= Node.val <= 100
  • 进阶:*你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)
阅读更多

LeetCode[21] 合并两个有序链表

“递归”: https://leetcode.com/tag/recursion/ “链表”: https://leetcode.com/tag/linked-list/ Similar Questions: “合并K个升序链表”: https://leetcode.com/problems/merge-k-sorted-lists/ “合并两个有序数组”: https://leetcode.com/problems/merge-sorted-array/ “排序链表”: https://leetcode.com/problems/sort-list/ “最短单词距离 II”: https://leetcode.com/problems/shortest-word-distance-ii/

Problem:

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例 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
  • l1l2 均按 非递减顺序 排列
阅读更多