leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

24. 两两交换链表中的节点

Open buuing opened this issue 4 years ago • 0 comments

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

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

示例 1:

image

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

示例 2:

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

示例 3:

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

提示:

  • 链表中节点的数目在范围 [0, 100] 内
  • 0 <= Node.val <= 100

进阶:你能在不修改链表节点值的情况下解决这个问题吗?(也就是说,仅修改节点本身。)


来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/swap-nodes-in-pairs 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


  • 迭代解题
const swapPairs = head => {
  if (!head || !head.next) return head
  let curr = head, prev
  head = curr.next
  while (curr && curr.next) {
    next = curr.next
    if (prev) prev.next = next
    curr.next = next.next
    next.next = curr
    prev = curr
    curr = curr.next
  }
  return head
}
  • 递归解题
const swapPairs = head => {
  if (!head || !head.next) return head
  let curr = head, next = head.next
  let temp = next.next
  next.next = curr
  curr.next = swapPairs(temp)
  return next
}

buuing avatar May 29 '21 09:05 buuing