Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

/**
* 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||!head->next)
return head;
//申请哨兵节点
ListNode *h = (ListNode*)malloc(sizeof(struct ListNode));
h->next=head;
ListNode *s,*p,*q,*tmp;
s=h;
//两个节点中,p作为第一个节点,s是p前一个节点
p=s->next;
while(p){
//q作为第二个节点
q=p->next;
if(!q)
break;
p->next=q->next;
s->next=q;
q->next=p; s=p;
p=p->next;
}
p=h->next;
free(h);
return p;
}
};

Swap Nodes in Pairs的更多相关文章

  1. Leetcode-24 Swap Nodes in Pairs

    #24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  2. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  3. [LintCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head.   Example Given 1->2-> ...

  4. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  5. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  6. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  7. leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法

    Swap Nodes in Pairs  Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  8. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

  9. leetcode-algorithms-24 Swap Nodes in Pairs

    leetcode-algorithms-24 Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and re ...

  10. 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 ...

随机推荐

  1. -bash: fork: retry: Resource temporarily unavailable

    登陆不了服务器The server refused to start a shell. 登陆服务器后执行ls命令报错:   1 2 $ls -bash: fork: retry: Resource t ...

  2. Feature Access

    在ArcGIS Server中发布支持Feature Access地图服务,你需要知道的几点: 所绘制的mxd地图文件中包含的数据,必须来自企业级数据库链接: mxd中包含的所有图层的数据,必须来自同 ...

  3. 在Eclipse中集成Ant配置

    提要:本文将向你展示如何使用Eclipse设置为Ant所用的属性值和环境变量,并简要分析如何配置Ant编辑器以便从Eclipse内部操作Ant文件. 一. 修改Ant Classpath 在使用一个可 ...

  4. mysql查询时强制区分大小写

    转载自:http://snowolf.iteye.com/blog/1681944 平时很少会考虑数据存储需要明确字符串类型字段的大小写,MySQL默认的查询也不区分大小写.但作为用户信息,一旦用户名 ...

  5. java 部分隐藏字段

    项目中,很多时候要对数据作隐私保护,隐藏一些数据的关键信息,比如身份证,隐藏其中的出生年月 //利用正则表达式匹配替换字符串 String email = "young452wer@gami ...

  6. 在eclipse创建android project,最后一步点击finish没反应

    在创建android project的时候,到最后一步点击finish没有反应. 本来以为可能是SDK中的Extra下Android Support Library没有安装,后来检查发现Minimum ...

  7. STM32F412应用开发笔记之二:基本GPIO控制

    NUCLEO-F412ZG板子上的元器件并没有完全焊接,除去ST-LINK部分和电源部分后,还有用一个USB主机接口,三个LED灯和两个按钮,不过很多功能引脚都已经引到了插针.查看原理图可发现,由原理 ...

  8. git常用的命令集合

    Git 是一个很强大的分布式版本控制系统.它不但适用于管理大型开源软件的源代码,管理私人的文档和源代码也有很多优势. Git常用操作命令: 1) 远程仓库相关命令 检出仓库:$ git clone g ...

  9. 广义线性模型(Generalized Linear Models)

    前面的文章已经介绍了一个回归和一个分类的例子.在逻辑回归模型中我们假设: 在分类问题中我们假设: 他们都是广义线性模型中的一个例子,在理解广义线性模型之前需要先理解指数分布族. 指数分布族(The E ...

  10. 记录一下折腾webp 的过程

    最近有客户想要处理webp 的动图,情况当然是我们并不能处理webp 格式的图片.这事就交给了我来折腾,一开始想着用瑞士军刀ffmpeg.结果是折腾了差不多一天,前前后后编译了几十次ffmpeg 源码 ...