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. C++对象模型的那些事儿之三:默认构造函数

    前言 继前两篇总结了C++对象模型及其内存布局后,我们继续来探索一下C++对象的默认构造函数.对于C++的初学者来说,有如下两个误解: 任何class如果没有定义default constructor ...

  2. Android优化之ArrayMap

    ArrayMap的介绍 官方对ArrayMap也有说明:它不是一个适应大数据的数据结构,相比传统的HashMap速度要慢,因为查找方法是二分法,并且当你删除或者添加数据时,会对空间重新调整,在使用大量 ...

  3. 数值分析:Hermite多项式

    http://blog.csdn.net/pipisorry/article/details/49366047 Hermite埃尔米特多项式 在数学中,埃尔米特多项式是一种经典的正交多项式族,得名于法 ...

  4. spring struts2 ibatis 框架结构图

    spring struts2 ibatis 框架结构图

  5. EXCEPTION与ERROR的区别

    EXCEPTION与ERROR的区别

  6. 关于Debug和Release之本质区别的讨论

    一.Debug 和 Release 编译方式的本质区别     Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release 称为发布版本,它往往是进行了各种优化 ...

  7. Linux Shell 只列出目录的方法

    在实际应用中,我们有时需要仅列出目录,下面是 4 种不同的方法. 1. 利用 ls 命令的 -d 选项: $ ls -d */ Desktop/ pic/ shell/ src/ 2. 利用 ls 命 ...

  8. 开源视频平台:Kaltura

    Kaltura是一个很优秀的开源视频平台.提供了视频的管理系统,视频的在线编辑系统等等一整套完整的系统,功能甚是强大. Kaltura不同于其他诸如Brightcove,Ooyala这样的网络视频平台 ...

  9. 漫谈程序员(十八)windows中的命令subst

    漫谈程序员(十八)windows中的命令subst 用法格式 一.subst [盘符] [路径]  将指定的路径替代盘符,该路径将作为驱动器使用 二.subst /d 解除替代 三.不加任何参数键入  ...

  10. 内存数据网格hazelcast的一些机制原理

    hazelcast作为一个内存数据网格工具,还算比较优秀,听说有Apache顶级项目使用它,值得研究下,使用文档可以直接看官方文档,但机制原理相关的资料基本没有,本人硬撸源码写的一些东西,跟大家分享一 ...