24. Swap Nodes in Pairs[M]两两交换链表中的节点
题目
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
Example:
Given 1->2->3->4,you should return the list as 2->1->4->3.
思路
思路一:遍历交换
本题需要我们对链表进行两两的交换,并且
1.如何交换表头
这里已经用过很多遍了,在链表的表头增加一个辅助节点。
ListNode* result = new ListNode(-1);
result->next = head;
2.如何进行链表的两两交换
链表的交换涉及到三个节点:当前节点(cur),当前节点的下一个节点(first),当前节点的下下个节点(second),交换原理如图1:
图1:交换节点示意图
于是交换操作实现如下:
```cpp
first->next = second->next;
cur->next = second;
cur->next->next = first;
```
**3.如何判断是否可以进行交换**
链表的交换必须要保证
当前节点的下一个节点以及下下个节点都必须存在,因此增加判断条件。
###思路二:递归法
很容易就能想到,对链表的每两个节点进行交换,实际上就是一种递归操作,如图3。
图2:递归示意图
#C++
- 思路一
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == nullptr || nullptr)
return head;
ListNode* result = new ListNode(0);
result->next = head;
ListNode* cur = result;
while(cur->next != nullptr && cur->next->next != nullptr){
ListNode* first = cur->next;
ListNode* second = cur->next->next;
//节点交换
first->next = second->next;
cur->next = second;
cur->next->next = first;
cur = cur->next->next;
}
return result->next;
}
};
- 思路二
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == nullptr || head->next == nullptr)
return head;
ListNode* result = head -> next;
head->next = swapPairs(result->next);
result->next = head;
return result;
}
};
Python
参考
24. Swap Nodes in Pairs[M]两两交换链表中的节点的更多相关文章
- LeetCode 24. 两两交换链表中的节点(Swap Nodes in Pairs)
题目描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能 ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- NO.24两两交换链表中的节点
NO.24两两交换链表中的节点 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例:给定 1->2->3-&g ...
- Java实现 LeetCode 24 两两交换链表中的节点
24. 两两交换链表中的节点 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3-&g ...
- leetcode 24. 两两交换链表中的节点 及 25. K 个一组翻转链表
24. 两两交换链表中的节点 问题描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2-> ...
- 【LeetCode】24.两两交换链表中的节点
24.两两交换链表中的节点 知识点:链表 题目描述 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点.你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换). 示例 示例 1 ...
- lintcode-451-两两交换链表中的节点
451-两两交换链表中的节点 给一个链表,两两交换其中的节点,然后返回交换后的链表. 样例 给出 1->2->3->4, 你应该返回的链表是 2->1->4->3. ...
- LeetCode-024-两两交换链表中的节点
两两交换链表中的节点 题目描述:给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例说明请见LeetCode官网. 来源:力 ...
随机推荐
- SAP computer之input and MAR
Input and MAR Below the program counter is the input and MAR block. It includes the address and data ...
- 利用VMware14安装虚拟机(Win7&CentOS6.4)
安装Win7 https://blog.csdn.net/Yangchenju/article/details/80694597 安装CentOS6.4 https://blog.csdn.net/u ...
- NSURLCredential 代表认证结果证书?
NSURLCredential 代表认证结果证书?
- monkey测试环境搭建 及 操作步骤
1.环境搭建 a.下载安卓SDK 链接:https://pan.baidu.com/s/1-OB6UVPvl5-N-vFdykfMmA 提取码:3spx b.配置环境变量(配置完成,重启系统,配置生效 ...
- BZOJ 5466: [Noip2018]保卫王国 动态DP
Code: // luogu-judger-enable-o2 #include<bits/stdc++.h> #define ll long long #define lson (now ...
- kvm之 virt-install工具命令详解
一.virt-install是一个命令行工具,它能够为KVM.Xen或其它支持libvrit API的hypervisor创建虚拟机并完成GuestOS安装:此外,它能够基于串行控制台.VNC或SDL ...
- 什么是Capability
desired capability的功能是配置Appium会话.他们告诉Appium服务器您想要自动化的平台和应用程序. Desired Capabilities是一组设置的键值对的集合,其中键对应 ...
- 学习EXTJS6(8)基本功能-表单的基础表字段Ext.form.field.Basic
Ext.form.field.Basic是表单字段的基类. Ext.form.field.Text Ext.form.field.TextArea Ext.form.field.Number Ext. ...
- 【hihocoder 1473】小Ho的强迫症
[题目链接]:http://hihocoder.com/problemset/problem/1473 [题意] [题解] 假定初始为在在0位置(相对它左边那条线); 则考虑; 多少步之后,人又能这到 ...
- hdu 2089 数位dp入门题
#include<stdio.h> //dp[i][0]代表不存在不吉利数字 //dp[i][1]代表不存在不吉利数字但是以2开头 //dp[i][2]代表存在不吉利数字 #define ...