给定一个链表,对每两个相邻的结点作交换并返回头节点。
例如:
给定 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. BZOJ2733:[HNOI2012]永无乡

    浅谈线段树合并:https://www.cnblogs.com/AKMer/p/10251001.html 题目传送门:https://lydsy.com/JudgeOnline/problem.ph ...

  2. 洛谷【P1009】阶乘之和

    题目传送门:https://www.luogu.org/problemnew/show/P1009 高精度加法:https://www.cnblogs.com/AKMer/p/9722610.html ...

  3. Hyperledger fablic 0.6 在centos7环境下的安装与部署

    原文:http://blog.csdn.net/zhaoliang1131/article/details/54617274 Hyperledger Fabric超级账本 项目约定共同遵守的 基本原则 ...

  4. 开发框架:AdminLTE

    ylbtech-开发框架:AdminLTE 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部 1. 2. 5.返回顶部 1. https://adminlte.io 2.   6.返 ...

  5. 关于KMeans 最外围点移除实验(其中心保持不变)

    import matplotlib.pyplot as plt from sklearn.datasets import make_blobs import numpy as np X,labels ...

  6. SpringCloud01 服务提供者和消费者

    说明:服务消费者直接利用RestTemplate调用服务提供者,这种使用方式只是适用于微服务数量比较少的项目,如果微服务的数量比较多建议使用SpringCloud提供的Eureaka组件. 注意:实现 ...

  7. SQL标量值函数:返回汉字拼音首拼

    今天遇到一个需求,客户要求在系统客户端选择客户的时候,可以用拼音首拼去快速过滤选择,此时我们在客户表里面加多一个拼音首拼字段CustPY来记录,字段加好了,我们要把所有客户名称的拼音简拼都更新到Cus ...

  8. centos MAC 地址与报错eth0 unknown interface no such device

    eth0 unknown interface no such device 出现这个原因是由于虚拟机直接COPY过来,MAC地址发生了变化,但eth0 里仍然记录着旧的MAC地址. 解决方法: vim ...

  9. 6.7 通过终端连接jboss,连接数据库

    进入终端,通过 ssh jboss@ip地址即可. 然后通过sqlplus 数据库名,输入密码,即可连接数据库. 具体不懂的可以查看对应文档进行操作.

  10. 27.【转载】挖洞技巧:如何绕过URL限制

    大家对URL任意跳转都肯定了解,也知道他的危害,这里我就不细说了,过~ 大家遇到的肯定都是很多基于这样的跳转格式:http://www.xxx.xxx/xxx?xxx=http://www.xxx.x ...