Swap Nodes in Pairs(交换节点)
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&¤t.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(交换节点)的更多相关文章
- [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 ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- 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 ...
- 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 ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 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 ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
随机推荐
- Dalvik虚拟机
Dalvik虚拟机是google专门为android平台开发的一个java虚拟机,但它并没有使用JVM规范.Dalvik虚拟机主要完成对象生命周期的管理.线程管理.安全和异常管理以及垃圾回收等重要功能 ...
- C++三目运算符的增强
<p>// 在C语言中表达式的结果放在寄存器中 // 在C语言中,表达式的返回值是变量的值 // 在C++中,表达式返回的是变量的本身</p><pre name=&quo ...
- SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP
AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...
- javascript之事件模型
事件模型 冒泡型事件(Bubbling):事件由叶子节点沿祖先节点一直向上传递到根节点 捕获型事件(Capturing):由DOM树最顶元素一直到最精确的元素,与冒泡型事件相反 DOM标准事件模型:D ...
- [Linux]vbox 虚拟机添加新磁盘
情况是这样的,开始创建虚拟机的时候硬盘设置太小了,只有10g,我现在通过vbox的设置给这个linux(centos6.6)虚拟机添加了一块硬盘. 下面的操作就是怎么把硬盘挂载到系统中. 通过 fdi ...
- Unity UGUI基础之Button
UGUI Button,可以说是真正的使用最广泛.功能最全面.几乎涵盖任何模块无所不用无所不能的组件,掌握了它的灵巧使用,你就几乎掌握了大半个UGUI! 一.Button组件: Interactabl ...
- MO_GLOBAL - EBS R12 中 Multi Org 设计的深入研究 (2)
这是多组织访问的第二篇文章,翻译自Anil Passi的Multi Org R12 我们都知道,在Oracle Release 12中多组织模型(Multi Org)会被改变, 它被叫作多组织访问控制 ...
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十)
打开Arm.h,在其接口中添加一个新方法: -(void)armShoot; 接下来在Arm.m中实现该方法: -(void)armShoot{ CGPoint startPoint = [self ...
- Guava 教程(3):Java 的函数式编程,通过 Google Collections 过滤和调用
原文出处: oschina 在本系列博客的第一.二部分,我介绍了非常优秀的Google Collections和Guava包.本篇博客中我们来看看如何使用Google Collections来做到过滤 ...
- ios入门OC_UI晋级学什么?
1. OC 语法初步, 你可能学到面向对象最近本的概念, 并且可以大致的建立几个自以为是的类,但这仅仅是开始. 你知道为什么面向对象要有3大特性么.知道他们是用到什么设计模式的么 2. 你可能学到了N ...