链表递归-leetcode两两交换相邻链表中的结点

两两交换相邻链表中的结点

题目:

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

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

示例1

在这里插入图片描述

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

示例 2:

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

示例 3:

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

题解:

迭代

设置一个虚拟头结点,第一次设置为first结点 后面的连续两个结点设为second和third

在这里插入图片描述

/**
 * 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,head);
        ListNode*first=dummyHead;
        while(first->next!=nullptr&&first->next->next!=nullptr)
        {
            ListNode*third=first->next->next;
            ListNode*second=first->next;
            second->next=third->next;  //二号结点连接三号节点的下一个即四号节点地址
            first->next=third; //一号结点连接三号节点
            third->next=second;  //三号结点连接二号节点
            first=first->next->next;  //一号结点向后移动两步
            }
        return dummyHead->next;
    }
};

递归法

/**
 * 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) {
        if(head==nullptr||head->next==nullptr)
        {
            return head;
        }
        ListNode *newhead=head->next;
        head->next=swapPairs(newhead->next);
        newhead->next=head;
        return newhead;
    }
};

递归法展开:(以五个结点的链表为例)

在这里插入图片描述

在这里插入图片描述

swapPairs(ListNode* head) {//①
    //第一层递归
    newhead =head->next;
    head->next=swapPairs(newhead->next);//② head->next=newhead->next->next
          //第二层函数递归
         {newhead=head->next;
          head->next=swapPairs(newhead->head); //③ head->next=newhead->next 当前函数下的head和newhead
                  //第三层递归 直接有了返回值
                  {return head;//这里的head是上面函数的newhead->head  是swapPairs(newhead->head)函数的返回值(③)  赋值给上一层的head->next
                  }
          newhead->next=head;
          return newhead;  //是函数②的返回值
         }
    newhead->next= head;
	return newhead;//嵌套的函数全部有了返回值  这是最终的结果  ①的返回值
}

相关推荐

  1. leetcode24. 交换节点

    2024-03-21 22:50:02       48 阅读
  2. LeetCode [24] 交换节点

    2024-03-21 22:50:02       44 阅读
  3. Leetcode】24. 交换节点

    2024-03-21 22:50:02       44 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-21 22:50:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-21 22:50:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-21 22:50:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-21 22:50:02       20 阅读

热门阅读

  1. C语言打印当前时间

    2024-03-21 22:50:02       21 阅读
  2. linux开机启动设置方法

    2024-03-21 22:50:02       25 阅读
  3. 二、Eureka注册中心

    2024-03-21 22:50:02       17 阅读
  4. C语言—用题目辨析gets()与scanf()

    2024-03-21 22:50:02       22 阅读
  5. vue01

    2024-03-21 22:50:02       21 阅读
  6. SystemServer的面试题目

    2024-03-21 22:50:02       19 阅读