Total Accepted: 73777 Total Submissions: 219963 Difficulty: Medium

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) {
ListNode* first=head;
ListNode* second = first ? first->next:NULL;
ListNode* next=NULL,*pre=NULL;
while(first && second){
next = second->next;
second->next = first;
first->next = next;
pre ? pre->next = second: head = second;//有前驱的话,前驱的下一几点指向交换后的两个节点的头,否则,为第一次交换,更新头结点.
pre = first;
first=next;
second = first ? first->next:NULL;
}
return head;
}
};

[Linked List]Swap Nodes in Pairs的更多相关文章

  1. leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现

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

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

  3. 24. Swap Nodes in Pairs

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

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

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

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

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

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

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

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

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

  9. Leetcode 线性表 Swap Nodes in Pairs

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

随机推荐

  1. 不同SQL Server数据库之间的跨数据库查询

    --不同SQL Server数据库之间的跨数据库查询 EXEC sp_addlinkedserver @server=N'OldDatabase', --自己定义别名 @srvproduct=N'', ...

  2. Spring 入门 AOP

    通过一个小例子演视怎么使用 Spring 现实面向切面编程. 导入 Spring 所需要的包 spring-framework-2.5.6 版需要导入以下包: 1.----- spring.jar 2 ...

  3. 封装一些数据库SQLCipher的方法(增、删、改、查)

    上一篇随笔只是简单的说了一下使用SQLCipher框架,介绍的比较笼统,可能看一遍之后更加蒙圈了,为了更好的使用这个数据库,整理了我在公司项目的需要用的方法,包括创建表,插入数据,更新数据,搜索查询数 ...

  4. canvas入门

    <html> <head> <script> window.onload=function(){ var canvas=document.getElementByI ...

  5. SpringMVC4.0以后版本返回json格式数据问题

    第一次写博文写的不好,但希望能帮助大家,有什么偏颇的地方希望大家多多斧正.在这个问题上困扰了我两天,这两天翻来覆去睡不着.一直在想这个问题.废话不多说下面进入正题. 1.创建创建web项目,加入Spr ...

  6. python 基础篇(二)数据类型概述

    正式进入python的学习. 数据类型可以分为身份,类型,数据项三项联合组成. 身份: id() 类型:type() 数据类型:int,boolean,tuple,string,dict,list 1 ...

  7. MYSQL分页存储过程及事务处理--转自peace

    MYSQL的分页过程,和事务处理的一个测试过程. /* --名称:MYSQL版查询分页存储过程 by peace 2013-8-14 --输入参数:@fields -- 要查询的字段用逗号隔开 --输 ...

  8. Words-specialty

    1-100   101-200   community n.社区; 共同体; 社会团体; [生态] 群落 overview n.概观; 总的看法; 回顾,复习 transforming vi.改变,使 ...

  9. knockout简单实用教程2

    在上一篇文章中简单了介绍了下什么ko(后文中都已ko来代替knockout.js)和一些简单的ko的使用方法下面我将介绍在实际的项目中常用到的几种绑定方式和方法. 在开始之前先拿一个dome来回顾下k ...

  10. android的listview组件

    http://www.cnblogs.com/menlsh/archive/2013/03/15/2962350.html http://www.tuicool.com/articles/IveeI3 ...