给定一个链表,对每两个相邻的结点作交换并返回头节点。
例如:
给定 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. Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)

    一.题目大意 本题要求写出前5482个仅能被2,3,5, 7 整除的数. 二.题解 这道题从本质上和Poj 1338 Ugly Numbers(数学推导)是一样的原理,只需要在原来的基础上加上7的运算 ...

  2. HDOJ1059(多重部分和问题)

    #include<cstdio> #include<cstring> using namespace std; +; ]; int dp[SIZE]; bool check() ...

  3. HDOJ1059(多重背包)

    1.解法一:多重背包 #include<iostream> #include<cstdio> using namespace std; #define MAX(a,b) (a& ...

  4. loadrunner手动生成脚本函数

    1.点击insert

  5. RPM包及其管理 rpm命令

    一.什么是RPMRPM:RedHat Package Manager     //红帽包管理如果Linux发行版本是redhat .redflag .centos .fedora .suse等或者衍生 ...

  6. Python-Redis的String操作

    Ubuntu安装Redis sch01ar@ubuntu:~$ sudo apt install redis-server sch01ar@ubuntu:~$ redis-server sch01ar ...

  7. 关于JAVA中的回调接口传值机制

    详见博文http://blog.csdn.net/xiaanming/article/details/8703708

  8. hadoop自动安装脚本

    还不能实现完全自动安装,只能算半自动的. 进行交互主要障碍有两点: 1. ssh-keygen的时候需要点击回车. 2. passwd 需要设置密码  如果谁能解决以上两点,欢迎email给我. 另外 ...

  9. geneid/genesymbol/ensemblid等之间的转换

    在基因注释时,难免碰到各种GENE在不同数据库之间的ID转换(例如,Ensembl ID 转Entrez ID,或者Entrez ID与GENE Symbol之间的转换).这里介绍一下常用的三个在线网 ...

  10. NIO和Reactor

    本文参考Doug Lea的Scalable IO in Java. 网络服务 随着网络服务的越来越多,我们对网络服务的性能有了更高的要求,提供一个高性能,稳定的web服务是一件很麻烦的事情,所以有了n ...