链表操作的,要注意标记头结点和边界问题。

代码如下:

 ListNode *swapPairs(ListNode *head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* p=head;
ListNode* q=head->next;
ListNode* x=head->next->next;
ListNode* t=NULL;
head=q;
while(p!=NULL&&q!=NULL){
q->next=p;
p->next=NULL;
if(t!=NULL)
t->next=q;
t=p; p=x;
if(x!=NULL)
q=x->next;
if(q!=NULL)
x=q->next; }
if(p!=NULL)
t->next=p;
return head;
}

更加巧妙的解法,直接对链表中节点的val进行交换,不用操作链表;

代码如下:

 /**
* 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) {
int n;
ListNode* node = head;
while (head != NULL && head->next != NULL) {
n = head->val;
head->val = head->next->val;
head->next->val = n;
head = head->next->next;
}
return node;
}
};

leetcode 24的更多相关文章

  1. [LeetCode] 24 Game 二十四点游戏

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  2. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  3. Java实现 LeetCode 24 两两交换链表中的节点

    24. 两两交换链表中的节点 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3-&g ...

  4. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  5. Leetcode: 24 Game

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  6. LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)

    题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...

  7. leetcode 24. 两两交换链表中的节点 及 25. K 个一组翻转链表

    24. 两两交换链表中的节点 问题描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2-> ...

  8. [leetcode 24] Swap Nodes in k-Group

    1 题目: 目前被墙,看不了. 2 思路: 比较简单,注意处理边界点就好 3 代码: public ListNode swapPairs(ListNode head) { int temp; if(h ...

  9. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

随机推荐

  1. Existence and nonexistence results for anisotropic quasilinear elliptic equations

    Fragalà, Ilaria; Gazzola, Filippo; Kawohl, Bernd. Existence and nonexistence results for anisotropic ...

  2. linux shell 中的位置变量

    对于linux shell 中的位置变量,我一直以来都是吐不出来又咽不下去,每次看到都不懂,不懂了就去百度google,看完了又忘,真是慢性咽炎啊.现在认真想想也是,其实自己一直以来都没有好好学习过, ...

  3. vim常用命令笔记(转载)

    添加多行注释:   1. 首先按esc进入命令行模式下,按下Ctrl + v,进入列(也叫区块)模式;   2. 在行首使用上下键选择需要注释的多行;   3. 按下键盘(大写)“I”键,进入插入模式 ...

  4. Python进阶07 函数对象

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 秉承着一切皆对象的理念,我们再次回头来看函数(function).函数也是一个对象 ...

  5. Ubuntu下tftp服务搭建

    1.安装软件包 sudo apt-get install tftpd tftp xinetd 2.建立配置文件 在/etc/xinetd.d/下建立一个配置文件tftp sudo vi /etc/xi ...

  6. Dede cms文章内容管理系统安全漏洞!如何有效防止DEDE织梦系统被挂木马安全设置

    第一.安装Dede的时候数据库的表前缀,最好改一下,不要用dedecms默认的前缀dede_,可以改成ljs_,随便一个无规律的.难猜到的前缀即可. 第二.后台登录一定要开启验证码功能,将默认管理员a ...

  7. android 插件化 模块化开发

    http://blog.csdn.net/o1587790525/article/details/11891997 Android 插件化架构设计  http://www.iqiyi.com/w_19 ...

  8. [ActionScript 3.0] AS3 深入理解Flash的 应用程序域Application Domains

    简介 网上有很多flash,通常都不需要显示的使用应用程序域,因为默认的应用程序域就够用了.其实复杂的情况下需要用到应用程序域,比如说有两个不同的swf,一个是旧版本的,一个是新版的,这两个文件里的类 ...

  9. ArcGIS Engine 图层裁剪 Clip的实现方法

    方法一, 图层对图层裁剪,输出图层 ILayer pLayer; IFeatureLayer pFeatureLayer; IFeatureClass pFeatureClass; IWorkspac ...

  10. ArcGIS栅格数据的合并和剪切

    ArcGIS栅格数据的合并和剪切 1.  合并:ArcToolBox-- àDataManagement--à Raster--àRaster Dataset--à Mosaic 或 Mosaic t ...