【中等】24. 两两交换链表中的节点

题目

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

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

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

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

提示:

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

解题

基本链表操作,并不难

func swapPairs(head *ListNode) *ListNode {
    dummy := &ListNode{Next:head}

    cur := dummy

    for cur.Next!=nil && cur.Next.Next!=nil {
        one, two := cur.Next, cur.Next.Next
        one.Next = two.Next
        cur.Next = two
        two.Next = one
        cur = one

    }

    return dummy.Next
}
func swapPairs(head *ListNode) *ListNode {

    if head==nil || head.Next == nil{
        return head
    }

    tail := head.Next
    head.Next = swapPairs(tail.Next)
    tail.Next = head
    return tail
}

题解

与题解很像了,真八错 (*^▽^*)
实际上也确实没啥可出入的了……

方法一:递归

func swapPairs(head *ListNode) *ListNode {
    if head == nil || head.Next == nil {
        return head
    }
    newHead := head.Next
    head.Next = swapPairs(newHead.Next)
    newHead.Next = head
    return newHead
}

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

时间:O(n)
空间:O(n)

方法二:迭代

func swapPairs(head *ListNode) *ListNode {
    dummyHead := &ListNode{0, head}
    temp := dummyHead
    for temp.Next != nil && temp.Next.Next != nil {
        node1 := temp.Next
        node2 := temp.Next.Next
        temp.Next = node2
        node1.Next = node2.Next
        node2.Next = node1
        temp = node1
    }
    return dummyHead.Next
}

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

时间:O(n)
空间:O(1)

长进

  1. 多考虑考虑递归法
  • 9
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是交换链表节点的C语言代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct Node { int data; struct Node* next; } Node; // 定义交换链表节点的函数 void swapNodes(Node** head, int x, int y) { // 如果x和y相同,直接返回 if (x == y) { return; } // 定义需要交换节点和它们的前驱节点 Node *prevX = NULL, *currX = *head; Node *prevY = NULL, *currY = *head; // 寻找需要交换节点和它们的前驱节点 while (currX != NULL && currX->data != x) { prevX = currX; currX = currX->next; } while (currY != NULL && currY->data != y) { prevY = currY; currY = currY->next; } // 如果x或y不存在于链表,直接返回 if (currX == NULL || currY == NULL) { return; } // 如果x不是头节点,更新前驱节点的next指针 if (prevX != NULL) { prevX->next = currY; } else { // 如果x是头节点,更新头指针 *head = currY; } // 如果y不是头节点,更新前驱节点的next指针 if (prevY != NULL) { prevY->next = currX; } else { // 如果y是头节点,更新头指针 *head = currX; } // 交换节点的next指针 Node* temp = currY->next; currY->next = currX->next; currX->next = temp; } // 定义输出链表的函数 void printList(Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { // 创建一个测试用例链表 Node* head = (Node*)malloc(sizeof(Node)); head->data = 1; head->next = (Node*)malloc(sizeof(Node)); head->next->data = 2; head->next->next = (Node*)malloc(sizeof(Node)); head->next->next->data = 3; head->next->next->next = (Node*)malloc(sizeof(Node)); head->next->next->next->data = 4; head->next->next->next->next = NULL; // 输出原始链表 printf("原始链表: "); printList(head); printf("\n"); // 交换节点 swapNodes(&head, 2, 4); // 输出交换后的链表 printf("交换后的链表: "); printList(head); printf("\n"); return 0; } ``` 以上代码将创建一个测试用例链表,输出原始链表交换节点,输出交换后的链表。可以根据自己的需要修改测试用例链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值