【两两交换链表中的节点】

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

文章目录

思路

把第一步的模拟过程的步骤记录下来
一共分为三个步骤

在这里插入图片描述

解题方法

创建虚拟头节点
循环什么时候结束,需要考虑问题

Q:

  1. 奇数链表结束条件?
  2. 偶数链表结束条件?
  3. 为什么结束条件是与?

A:

  • cur->next为空结束

  • cur->next->next为空结束

  • 如果是cur->next != NULL || cur->next->next != NULL
    则当链表为奇数链表时,cur->next != NULL也成立,不符合条件。(根据模拟过程,如果要改变1和2的位置,指针cur需要指向前一个位置,即dummy,同理,改变3和4位置,cur指向2,当3和4交换完毕,cur会指向4,再进行条件判断)
    在这里插入图片描述
    注意要点
    cur->next != NULLcur->next->next != NULL顺序不能错,否则会出现空指针异常
    以下是链表模拟过程
    步骤一
    在这里插入图片描述
    步骤2
    在这里插入图片描述
    步骤3
    在这里插入图片描述

Code

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* dummyHead = new ListNode(0);
        dummyHead->next = head;
        ListNode* cur = dummyHead;

        while (cur->next != NULL && cur->next->next != NULL) // 注意是与,并且顺序不能错,不然就异常
        {
            ListNode* tmp = cur->next; // 把步骤2的内容存下
            ListNode* tmp1 = cur->next->next->next; // 把步骤3的内容存下

            cur->next = cur->next->next; // 步骤1
            cur->next->next = tmp; // 步骤2
            cur->next->next->next = tmp1; // 步骤3

            cur = cur->next->next;
        }
        head = dummyHead->next;
        delete dummyHead;
        return head;
        
    }
};
### 两两交换链中的节点算法实现 在解决两两交换链中的节点问题时,可以采用递归或迭代的方法。以下是两种方法的详细说明和代码实现。 #### 方法一:递归法 递归方法的核心思想是将链表分为当前需要交换的两个节点和剩余链表两部分。首先判断链表是否为空或只有一个节点,如果是,则直接返回头节点。然后递归调用 `swapPairs` 函数处理剩余的节点,并更新指针关系[^2]。 ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def swapPairs(self, head: ListNode) -> ListNode: # 如果链表为空或只有一个节点,直接返回 if not head or not head.next: return head # 获取当前节点的下一个节点 p = head.next # 递归调用处理剩余节点 head.next = self.swapPairs(p.next) # 将 p 的 next 指向当前节点 p.next = head # 返回新的头节点 p return p ``` #### 方法二:迭代法 迭代方法通过引入一个虚拟头节点(dummy node)来简化指针操作。使用一个临时指针 `temp` 遍历链表,每次交换 `temp.next` 和 `temp.next.next` 两个节点,并更新指针关系[^4]。 ```python class Solution: def swapPairs(self, head: ListNode) -> ListNode: # 创建虚拟头节点 dummy = ListNode(0) dummy.next = head # 使用 temp 指针遍历链表 temp = dummy while temp.next and temp.next.next: # 定义需要交换的两个节点 node1 = temp.next node2 = temp.next.next # 更新指针关系 temp.next = node2 node1.next = node2.next node2.next = node1 # 移动 temp 指针到下一个待交换的节点前 temp = node1 # 返回新的头节点 return dummy.next ``` ### 算法复杂度分析 - **时间复杂度**:无论是递归还是迭代方法,都需要遍历整个链表,因此时间复杂度为 O(n),其中 n 是链表节点数。 - **空间复杂度**: - 递归方法的空间复杂度为 O(n),因为递归调用会占用栈空间。 - 迭代方法的空间复杂度为 O(1),因为它只使用了常数个额外变量。 ### 注意事项 在实现过程中,需要注意边界条件的处理,例如链表为空或只有一个节点的情况。此外,在交换节点时,确保正确更新指针关系,避免出现断链或死循环的问题[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值