本文为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. php怎样求一个数组中最长的

    <?php $arr = array( 0 => 'd', 1 => '68b3', 2 => 'a86', 3 => 'c9aa97b23b71d5c', 4 => ...

  2. JavaSE学习总结第23天_多线程1

      23.01  多线程程序的引入 如果一个程序只有一个执行流程,所以这样的程序就是单线程程序. 如果一个程序有多条执行流程,那么,该程序就是多线程程序. 23.02  进程概述及多进程的意义 要想说 ...

  3. linux中grep的用法

    http://www.9usb.net/200902/linux-grep.html http://blog.51yip.com/linux/1008.html http://blog.csdn.ne ...

  4. 删除 Windows Azure 网站上的标准服务器头

    编辑人员注释: 本文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 请求和响应中包含的 HTTP 头是Web 服务器和浏览器之间的 HTTP 通信过程的一部分.例 ...

  5. Spout的实现步骤

    Spout的实现步骤: ·        对文件的改变进行分开的监听,并监视文件夹下有无新日志文件加入. ·        在数据得到了字段的说明后,将其转换成tuple. ·        声明Sp ...

  6. 【Web】HttpServletRequest request 相关方法 得到路径

    username=qqqq password=wwww //----------------------POST-------------------> http://192.168.1.211 ...

  7. vim下设置tab

    前言:大多数情况下tab键的宽度设置为4个空格,这个可以根据自己 的代码风格进行替换,然而当你提交不同的语言的代码的时候python 和c的时候就有区别了.c的话一般tab键做缩进,而python提交 ...

  8. java.lang.NoClassDefFoundError: org.ksoap2.transport.HttpTransportSE异常处理

    原因就是没有打包进去  因为引用进去 编译时没出出现问题 解决如下

  9. XCode 6 出现 no identity found: Command /usr/bin/codesign failed with exit code 1 解决方法汇总

    1, 解决办法,进入开发者账号重建一个 Provisioning Profiles(或配套证书) 文件,把证书添加正确就可以了 (应该是最有效的) 2, 将p12文件重新安装下 3, 在 iPhone ...

  10. ThinkPHP验证码类

    //ThinkPHP验证码类使用$config = array( 'fontSize' => 30, // 验证码字体大小 'length' => 3, // 验证码位数 'useNois ...