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. 06 Activity显示跳转

    <span style="font-size:18px;">package com.fmy.day8_29task; import com.fmy.day8_29tas ...

  2. 06 获取Activity的栈管理器

    代码 <span style="font-size:18px;">package com.fmy.day8_29task.util; import java.util. ...

  3. JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习

    JAVA之旅(二十九)--文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现 ...

  4. INV_TXN_MANAGER_PUB.PROCESS_TRANSACTIONS

    For Interface Transactions,INV_TXN_MANAGER_PUB.PROCESS_TRANSACTIONS DOES below things: 1)validate_gr ...

  5. Android进阶(二十八)上下文菜单ContextMenu使用案例

    上下文菜单ContextMenu使用案例 前言 回顾之前的应用程序,发现之前创建的选项菜单无法显示了.按照正常逻辑来说,左图中在"商品信息"一栏中应该存在选项菜单,用户可进行分享等 ...

  6. Java进阶(二)文件读操作

    本文以实际的读取文件为例子,介绍流的概念,以及输入流的基本使用. 按照前面介绍的知识,将文件中的数据读入程序,是将程序外部的数据传入程序中,应该使用输入流--InputStream或Reader.而由 ...

  7. C++ Primer 有感(异常处理)(二)

    异常就是运行时出现的不正常,例如运行时耗尽了内存或遇到意外的非法输入.异常存在于程序的正常功能之外,并要求程序立即处理.不能不处理异常,异常是足够重要的,使程序不能继续正常执行的事件.如果找不到匹配的 ...

  8. IT职场: 选择外企利与弊

    前几天有个同学打电话问我选择国内企业与外企的利弊,很可笑的是他是学机械的:既然和我完全不在一个行业,因此我只是说了我们IT外企的利与弊,毕竟隔行如隔山. 首先简单自我介绍一下,我所在的公司是美资500 ...

  9. javascript之prototype原型属性

    这个地方有点绕,仔细理解代码的意义. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

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

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