题目: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. SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.了解SpringBoot的基本概念 2.具体内容 在之前所建立的 SpringBoot 项目只是根据官方文档实现的一个基础程 ...

  2. c# 生成随机时间

    Random random = new Random((int)(DateTime.Now.Ticks)); ; ) { , ); , ); , ); string tempStr = string. ...

  3. (转)android系统架构及源码目录结构

    转自:http://blog.csdn.net/finewind/article/details/46324507 1. Android系统架构: android系统架构采用了分层架构的思想,如下图所 ...

  4. Keystone-manage 命令

    本文介绍Icehouse发行版的keystone-manage命令 keystone-manage是用来同keystone服务进行交互的命令行工具,通常该命令只用于不能通过HTTP API完成的操作, ...

  5. win10 .net framework 3.5无法安装错误代码0x800F081F

    复制链接:http://download.windowsupdate.com/d/msdownload/update/software/updt/2015/11/microsoft-windows-n ...

  6. Riak

    出处:http://www.oschina.net/p/riak Riak是以 Erlang 编写的一个高度可扩展的分布式数据存储,Riak的实现是基于Amazon的Dynamo论文,Riak的设计目 ...

  7. VS2015 applicationhost.config 本地域名访问

    .vs\config\applicationhost.config <?xml version="1.0" encoding="UTF-8"?> & ...

  8. ASP.NET MVC Castle Windsor 教程

    一.[转]ASP.NET MVC中使用Castle Windsor 二.[转]Castle Windsor之组件注册 平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其 ...

  9. JSP内置对象—session

    什么是session? session对象是用来在每个用户之间分别保存每个用户信息的对象,以便跟踪用户的操作状态.session的信息保存在server端,session的id保存在client的co ...

  10. [WPF打印]WPF 文档元素(Run TextBlock Paragraph)的文字对齐方式

    最近开发WPF程序,需要打印,用到了FlowDocument(这相当于有了打印模版,而且可以随时修改,真的是挺方便的).可是在输出表格形数据(这种情况恐怕是大多数~)时遇到了点儿麻烦. 由于Table ...