LeetCode-24. 两两交换链表中的节点【递归 链表】

LeetCode-24. 两两交换链表中的节点【递归 链表】

题目描述:

给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。

示例 1:
在这里插入图片描述
输入:head = [1,2,3,4]
输出:[2,1,4,3]
示例 2:

输入:head = []
输出:[]
示例 3:

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

提示:

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

解题思路一:双指针1

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head: return head
        dummy_head = ListNode(next=head)
        pre = dummy_head
        cur = dummy_head.next
        while cur.next:
            temp = cur.next
            pre.next = temp
            cur.next = temp.next
            temp.next = cur
            if cur.next == None:
                break
            pre = cur
            cur = cur.next

        return dummy_head.next

时间复杂度:O(n)
空间复杂度:O(1)

解题思路二:双指针2

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head == None or head.next == None: return head
        dummyHead = ListNode()
        dummyHead.next = head
        cur = dummyHead
        while cur.next != None and cur.next.next != None:
            tmp = cur.next
            tmp1 = cur.next.next.next

            cur.next = cur.next.next
            cur.next.next = tmp
            cur.next.next.next = tmp1

            cur = cur.next.next # 每次前进两个,并且判断后面有两个元素就交换
        return dummyHead.next

时间复杂度:O(n)
空间复杂度:O(1)

解题思路三:递归

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def swapPairs(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if head is None or head.next is None: return head

        pre = head
        cur = head.next
        next = head.next.next

        cur.next = pre
        pre.next = self.swapPairs(next)
        return cur

时间复杂度:O(n)
空间复杂度:O(n)

相关推荐

  1. leetcode24. 交换节点

    2024-04-03 16:00:01       47 阅读
  2. LeetCode [24] 交换节点

    2024-04-03 16:00:01       44 阅读
  3. Leetcode24. 交换节点

    2024-04-03 16:00:01       42 阅读
  4. LeetCode24.交换节点

    2024-04-03 16:00:01       40 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-03 16:00:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-03 16:00:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-03 16:00:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-03 16:00:01       18 阅读

热门阅读

  1. 5G时代来了,一键登录的颠覆式体验时代也来了

    2024-04-03 16:00:01       13 阅读
  2. ES 7.12官网阅读-ILM(index lifecycle management)

    2024-04-03 16:00:01       12 阅读
  3. LEETCODE-DAY41

    2024-04-03 16:00:01       13 阅读
  4. c++ 实现线程池、实现异步接口

    2024-04-03 16:00:01       13 阅读
  5. LeetCode 746. 使用最小花费爬楼梯

    2024-04-03 16:00:01       13 阅读
  6. 模拟退火算法

    2024-04-03 16:00:01       11 阅读
  7. 每日OJ题_回文串dp①_力扣647. 回文子串

    2024-04-03 16:00:01       10 阅读
  8. 【WPF应用24】C#中的Image控件详解与应用示例

    2024-04-03 16:00:01       14 阅读
  9. rust实现希尔排序算法

    2024-04-03 16:00:01       14 阅读
  10. 七彩云转码系统v12.8二开正式版发布

    2024-04-03 16:00:01       12 阅读
  11. 宝塔面板CentOS Stream 8 x86 下如何安装openlitespeed

    2024-04-03 16:00:01       11 阅读