给定一个链表,对每两个相邻的结点作交换并返回头节点。
例如:
给定 1->2->3->4,你应该返回 2->1->4->3。
你的算法应该只使用额外的常数空间。不要修改列表中的值,只有节点本身可以​​更改。

详见:https://leetcode.com/problems/swap-nodes-in-pairs/description/

实现语言:Java

方法一:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head==null){
return null;
}
ListNode dummy=new ListNode(-1);
ListNode pre=dummy;
dummy.next=head;
while(pre.next!=null&&pre.next.next!=null){
ListNode last=pre.next.next;
pre.next.next=last.next;
last.next=pre.next;
pre.next=last;
pre=last.next;
}
return dummy.next;
}
}

方法二:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode tmp=head.next;
head.next=swapPairs(tmp.next);
tmp.next=head;
return tmp;
}
}

参考:http://www.cnblogs.com/grandyang/p/4441680.html

024 Swap Nodes in Pairs 交换相邻结点的更多相关文章

  1. LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点

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

  2. LeetCode 024 Swap Nodes in Pairs

    题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  3. LeetCode Swap Nodes in Pairs 交换结点对(单链表)

    题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...

  4. 【LeetCode】024. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  5. Java for LeetCode 024 Swap Nodes in Pairs

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

  6. [leetcode]24. Swap Nodes in Pairs交换链表的节点

    感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...

  7. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  8. Reverse Nodes In K Group,将链表每k个元素为一组进行反转---特例Swap Nodes in Pairs,成对儿反转

    问题描述:1->2->3->4,假设k=2进行反转,得到2->1->4->3:k=3进行反转,得到3->2->1->4 算法思想:基本操作就是链表 ...

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

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

随机推荐

  1. Python 写文件时的Unicode设置

    今天在把Evenote的笔记内容写为文件时出错:     f.write(content) UnicodeEncodeError: 'gbk' codec can& ...

  2. django基础PROJECT APP View template

    project 和 app 的区别就是一个是配置另一个是代码: 一个project包含很多个Django app以及对它们的配置. 一个project的作用是提供配置文件,比方说哪里定义数据库连接信息 ...

  3. 2013 蓝桥杯校内选拔赛 java本科B组(题目+答案)

    一.标题:正则表示     正则表达式表示了串的某种规则或规律.恰当地使用正则表达式,可以使得代码简洁.事半功倍.java的很多API都支持正则表达式作为参数.其中的String.split就是这样. ...

  4. unittest 执行测试脚本输出测试报告

    import unittestimport HTMLTestRunnertest as HTMLTestRunner#获取路径path = './'#创建测试套件,读取测试脚本suite = unit ...

  5. Lua虚拟机初始化

    转自:http://www.cnblogs.com/ringofthec/archive/2010/11/09/lua_State.html 1. 创建lua虚拟机 lua_State *lua_ne ...

  6. 问题:C# params类型参数;结果:C#的参数类型:params、out和ref

    C#的参数类型:params.out和ref PS:由于水平有限,难免会有错误和遗漏,欢迎各位看官批评和指正,谢谢~ 首先回顾一下C#声明一个方法的语法和各项元素,[]代表可选 [访问修饰符] 返回值 ...

  7. url传参解决中文乱码

    跳转前: window.open("http://localhost:9728/content/agent/devolution.html?search_agent=" + enc ...

  8. sharepoint Foundation 2013安装过程

    安装完必备软件后,便可安装sharepoint Foundation 2013

  9. 关于android中两种service的编写简单总结

    1.startservice (两种方法,继承service类或者继承intentservice 类) 继承service类,在onstartcommend重载方法中实现业务逻辑的处理,如果耗时过长最 ...

  10. spring----IOC注解方式以及AOP

    技术分析之Spring框架的IOC功能之注解的方式 Spring框架的IOC之注解方式的快速入门 1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:S ...