题目: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.

代码:

 struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {} };
ListNode *swapPairs(ListNode *head)
{
if (head == NULL || head->next == NULL)
return head;
ListNode *p = head;
ListNode *q = p->next;
while (q)
{
int temp;
temp = p->val;
p->val = q->val;
q->val = temp;
if (q->next != NULL)
{
p = q->next;
if (p->next != NULL)
q = p->next;
else
break; }
else
break;
}
return head;
}

【LeetCode OJ】Swap Nodes in Pairs的更多相关文章

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

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

  2. LeetCode OJ 24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  3. LeetCode OJ:Swap Nodes in Pairs(成对交换节点)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  4. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  5. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

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

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

  7. [LeetCode 题解]:Swap Nodes in Pairs

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a li ...

  8. Leetcode 线性表 Swap Nodes in Pairs

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

  9. LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation

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

随机推荐

  1. 【转】【CentOS】【Python】Centos7安装Python3的方法

    由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_centos Python-3.6.2]# ...

  2. Android N: jack server failed

    在服务器上编译Android N.出现如下错误. Android N 编译时会使用到 jack server,同一台服务器上,各个用户都需要为 jack server 指定不同的端口,否则会产生端口冲 ...

  3. Java对象序列化给分布式计算带来的方便

    什么时候使用序列化: 一:对象序列化可以实现分布式对象.主要应用例如:RMI要利用对象序列化运行远程主机上的服务,就像在本地机上运行对象时一样.二:对象序列化不仅保留一个对象的数据,而且递归保存对象引 ...

  4. CSS z-index优先级

    可以设置div层与层之间的优先级,上下层覆盖优先情况. 当你定义的CSS中有position属性值为absolute.relative或fixed, 用z-index取值方可生效. 如果想要一个小的d ...

  5. [Learn AF3]第三章 App Framework 3组件之Panel:afui的核心

    Panel,afui的核心组件 组件名称:Panel     使用说明:按照官方的说法,panel组件是af3的“核心(heart of the ui)”,panel用于构造应用中独立的内容展示区域, ...

  6. Ubuntu下PHP动态编译出现Cannot find autoconf的解决方法

    执行phpize时出现Cannot find autoconf 错误 Ubuntu下解决方法 sudo apt-get install autoconf

  7. java字符集

    在utf-8编码中,unicode(编码字符集)是utf-8(字符编码)的表现形式 http://www.cnblogs.com/hanruyue/p/5859107.html

  8. Bootstrap 各种进度条详解

    一:默认的进度条 创建一个基本的进度条的步骤如下: 添加一个带有 class .progress 的 <div>. 接着,在上面的 <div> 内,添加一个带有 class . ...

  9. vim中文手册

    http://vimcdoc.sourceforge.net/doc/help.html

  10. SQLServer------远程调用失败

    1.情况 出现 2.解决方法 打开“控制面板” -> “卸载程序” -> 找到 “Microsoft SQL Server 2016) ExpressLocalDB”将其卸载 -> ...