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、使用递归,简单,空间不是固定的O(n)。

 public ListNode swapPairs(ListNode head) {
if(head == null||head.next==null) return head;
//递归,但是空间O(n)
ListNode node=head.next;
head.next=swapPairs(head.next.next);
node.next=head;
return node; }

2、直接遍历修改。。两个一组操作。因为头结点也会变,所以需要额外添加头结点。

 //非递归
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode current=dummy;
//最后如果只剩一个节点,不用交换,本身就在current上所以不作处理
while(current.next!=null&&current.next.next!=null){
ListNode first=current.next;
ListNode second=current.next.next;
first.next=second.next;
current.next=second;
current.next.next=first;
current=current.next.next;
}
return dummy.next;

Swap Nodes in Pairs(交换节点)的更多相关文章

  1. [leetcode]24. Swap Nodes in Pairs交换节点对

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

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

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

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

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

  5. 24. Swap Nodes in Pairs

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

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

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

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

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

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

  9. Leetcode 线性表 Swap Nodes in Pairs

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

随机推荐

  1. JQuery实战---初识JQuery+入门实例

    JQuery在小编的世界中,也就是JavaScript和查询(Query),即是辅助JavaScript开发的库,百度百科对JQuery的介绍比较详细,小伙伴可以东东自己可耐的小爪子,上网进行搜索,说 ...

  2. 小文本——Cookies

    http协议的无状态性导致在需要会话的场景下寸步难行,例如一个网站为了方便用户,在一段时间内登录过改网站的浏览器客户端实现自动登录,为实现这种客户端与服务器之间的会话机制需要额外的一些标识,http头 ...

  3. python的operator.itemgetter('click')用于定义获取'click'项的函数

    python的排序参见文章http://blog.csdn.net/longshenlmj/article/details/12747195 这里介绍 import operator模块 operat ...

  4. Android的原始资源Raw和Assert资源的使用-android学习之旅(五十七)

    代码示例 public class MainActivity extends Activity{ MediaPlayer mediaPlayer1,mediaPlayer2; @Override pr ...

  5. Android读取/dev/graphics/fb0 屏幕截图

    Android屏幕截图有很多方式这里只使用其中一种截图 主要是读取/dev/graphics/fb0,进行转换,复杂点就在如何把读取的数据进行转换. 可以参考一下这篇文章:http://blog.ch ...

  6. mysql filesort 的解决方案

    在explain我们所使用的sql的时候,经常会遇到using filesort这种情况,原以为是由于有相同列值的原因引起,结果昨天看到公司的一个sql,跟同事讨论了下加上自己又做了一些测试,突然发现 ...

  7. java设计模式---桥接模式

    桥接模式的目的是把抽象和具体实现分离,其uml类图如下所示: public interface SendMessage { abstract void send(String message,Stri ...

  8. OJ题:将一个字符串顺序翻转

    题目描述 写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串. 之前写过这样的一个程序,用位运算的方法去操作指针,但是那样的方法未免就有点复杂啦,不如用以下这种,简单明了. 程序如下: #i ...

  9. C++ Primer 有感(函数)

    1.函数应该在头文件中声明,并在源文件中定义.(定义函数的源文件应包含声明该函数的头文件)将提供函数声明的头文件包含在定义该函数的源文件中,可使编译器能检查该函数的定义和声明是否一致. 2.既可以在函 ...

  10. Dynamics CRM OData 查询超过50条记录的数据(Retrieving More than 50 records using OData)

    在通过ODdata方式获取CRM数据时,默认查询出来的results只有50条数据,可以通过JSON返回的Object中的"_next"属性作为URL循环获取直到该属性为空 示例代 ...