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. 嵌套iframe中的HTML的文档解析类型

    问题:页面整个刷新时,IE11输入框显示的宽度和高度正常,对页面中的iframe部分刷新时,IE11输入框的宽度和高度就变小了. 原因:网页的头部定义了文档类型<!DOCTYPE html> ...

  2. Sql Server获得每个表的行数

    SELECT o.[name], ddps.[row_count] FROM sys.indexes AS i INNER JOIN sys.objects AS o ON i.[object_id] ...

  3. C# 读取xml节点类容

    这是一个测试节点类容的获取 这是控制台代码部分 注意的应用文件 :using.system.Xml using System; using System.Collections.Generic; us ...

  4. tab标签切换(无炫效果,简单的显示隐藏)

    从最简单的效果开始写起,一个简单的JQ写出tab切换效果,很静态,没有任何的轮转特效,单纯的点击标签显示区域块. 附上代码: HTML: <div class="wrapper&quo ...

  5. Fragment 之 PagerAdapter

    package com.edaixi.main.adapter; import android.content.Context; import android.support.v4.view.Page ...

  6. plsql基本语法(

    1. 定义常量的语法格式    常量名 constant 类型标识符 [not null]:=值;    常量,包括后面的变量名都必须以字母开头,不能有空格,不能超过30个字符长度,同时不能和保留字同 ...

  7. Composer配置中国镜像

    用法: 有两种方式启用本镜像服务: 将以下配置信息添加到 Composer 的配置文件 config.json 中(系统全局配置).见“例1” 将以下配置信息添加到你的项目的 composer.jso ...

  8. 【写一个自己的js库】 4.完善跨浏览器事件操作

    1.阻止冒泡. function stopPropagation(event){ event = event || getEvent(event); if(event.stopPropagation) ...

  9. MEMS微加工技术

    MEMS的微加工有两种方法,一种是多层平面加工技术,还有一种是基于SOI的体加工技术. (一)多层平面加工技术 这种方法加工出来的结构有三层:作为主体的多晶硅层.作为暂时填充物的氧化物牺牲层以及多晶硅 ...

  10. eclipsecpp从可执行程序员中导入源代码并调试

    如果寻找跨平台IDE及调试环境,那eclipse是最佳选择了,因为它有一项超强功能:可以从可执行程序员导入源代码并调试程序,这种特性是非常适合自定义MakeFile项目,不需要改变项目现有结果,即可自 ...