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. (二)Netty源码学习笔记之服务端启动

    尊重原创,转载注明出处,原文地址:http://www.cnblogs.com/cishengchongyan/p/6129971.html  本文将不会对netty中每个点分类讲解,而是一个服务端启 ...

  2. SSH使用详解

    一.SSH基础 (1)什么是SSH? 传统的网络服务程序,如:ftp.pop和telnet在本质上都是不安全的,因为它们在网络上用明文传送口令和数据,别有用心的人非常容易就可以截获这些口令和数据.而且 ...

  3. Oracle10g 表分区

    1.分区的原因 (1)Tables greater than 2GB should always be considered for partitioning. (2)Tables containin ...

  4. jeecms3.0.4版本 详解请求如何找到首页(转)

    第一步:发送http://localhost:8080/emisstrade/ 请求 第二步:首先进入配置文件web.xml, <context-param> <param-name ...

  5. Linux 软件包管理

    简介: linux中软件包的管理随着linux版本的不同而不同,一般RPM和DPKG是最常见的两类软件包管理工具.分别应用基于rpm软件包的linux发行版本和基于deb软件包的linux发行版本. ...

  6. shell 输出九九乘法表

    #/bin/bash i= j= )) do while(($j<=$i)) do echo -ne $j×$i=$[$j*$i]"\t" j=$[$j+] done ech ...

  7. WPF 画心2.0版之元旦快乐

    2017年元旦已经到了,想做一个祝福语的窗口,就把上一篇画心的程序改了改,变成了如下界面. 说下改动的地方,首先窗口没有标题栏了. MainWindow.xaml AllowsTransparency ...

  8. vue2.0环境搭建

    1.安装node.js(官网) 2.安装淘宝镜像  npm install -g cnpm --registry=https://registry.npm.taobao.org 3.安装webpack ...

  9. 数据结构与算法之链表-javascript实现

    链表的定义: 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成.每个结点 ...

  10. DBCP连接池

    方法一: package DBCPUtils; import java.util.List;import java.util.Properties; import javax.sql.DataSour ...