本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Swap Nodes in Pairs

Total Accepted: 12511 Total
Submissions: 39302

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.

题意:交换给定链表中的相邻节点,但不能够改变链表里的值

如1->2->3->4交换后为2->1->4->3

思路:

按题意中的扫描去改变每两个相邻节点的next指针的指向就可以。

小技巧:

由于处理每两个相邻节点的时候,须要一个指针记录它们前一个节点,而头节点前面没有节点,

所以可设置一个dummy节点指向头指针,这样开头的两个节点的处理方式跟其他的相邻节点的处理方式就一样了

复杂度:时间O(n),空间O(1)

/**
* 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) return NULL;
ListNode *dummy = new ListNode(0); dummy->next = head;
ListNode *pre = dummy, *cur = head, *next = head->next;
while(cur && next){
ListNode *temp = next->next;
pre->next = next;
cur->next = next->next;
next->next = cur;
pre = cur;
cur = temp;
next = temp==NULL ? NULL : temp->next;
}
return dummy->next;
}

Leetcode 线性表 Swap Nodes in Pairs的更多相关文章

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

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

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

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

  3. 【LeetCode】24. Swap Nodes in Pairs (3 solutions)

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

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

  5. 【一天一道LeetCode】#24. Swap Nodes in Pairs

    一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...

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

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

  7. leetcode 题解 || Swap Nodes in Pairs 问题

    problem: Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...

  8. 【LeetCode】24. Swap Nodes in Pairs

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

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

随机推荐

  1. 配置分割Tomcat日志

    安装cronolog 官网下载:http://cronolog.org/usage.html ./configuremake;make install 或者 yum install cronolog ...

  2. HLS 协议

    HTML 5 视频直播一站式扫盲   本文来自于腾讯bugly开发者社区,原文地址:http://bugly.qq.com/bbs/forum.php?mod=viewthread&tid=1 ...

  3. maven GroupID和ArtifactID填什么

    GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构. ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称.一般Gro ...

  4. Spring Boot特性(转)

    摘要: 1. SpringApplication SpringApplication 类是启动 Spring Boot 应用的入口类,你可以创建一个包含 main() 方法的类,来运行 SpringA ...

  5. File中操作路径的API(转)

    这几天一直在搞Java,模板引擎系列和程序猿执业修养系列都暂停了,在Java上忙的不亦乐乎!由于对Java还不太熟悉,经历了各种纠结终于完成了任务.以下是关于Java获取当前目录的方法的备忘录. 原文 ...

  6. TPM 2.0 近况及模拟器开发

    可信计算平台模块TPM 2.0的相关标准和技术准则由 TCG ( Trust Computing Group )于2011年前后提出,至今已经过了多次修改.该标准无疑将成为下一代可信计算平台模块的业界 ...

  7. Vijos 1100 加分二叉树

    题目 1100 加分二叉树 2003年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB   题目描述 Description 设一个n个节点的二叉树tree的中序遍历为( ...

  8. opencv是什么

    OpenCV是一个用于图像处理.分析.机器视觉方面的开源函数库.       不管你是做科学研究,还是商业应用,opencv都能够作为你理想的工具库,由于,对于这两者,它全然是免费的.该库採用C及C+ ...

  9. ZOJ3640-Help Me Escape

    Help Me Escape Time Limit: 2 Seconds      Memory Limit: 32768 KB Background     If thou doest well, ...

  10. NYoj-街区最短路径问题

    街区最短路径问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 一个街区有非常多住户,街区的街道仅仅能为东西.南北两种方向. 住户仅仅能够沿着街道行走. 各个街道之 ...