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.

思路:题目比較简单,会链表反转的都能够做,思路也差点儿相同。不多说。上代码。

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode swapPairs(ListNode head) { ListNode firstHead = new ListNode(0);
firstHead.next = head;
ListNode pre = firstHead;//定义一个头结点,这样全部的操作都同样
ListNode next = null;
while(head != null && head.next != null){
//A-B-C-D交换BC,pre=A;B=head;C=next
next = head.next;//保存交换的变量C head.next = next.next;//将B指向B的指针指向D
pre.next = next;//将A指向B的指针指向C
next.next = head;//将C指向D的指针指向B,完毕交换,顺序变为A-C-B-D //为下一循环准备变量
pre = head;//将pre变为B
head = head.next;//将head指向D }
return firstHead.next;
}
}

leetCode 24. Swap Nodes in Pairs (双数交换节点) 解题思路和方法的更多相关文章

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

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

  2. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

  3. [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 ...

  4. LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  5. [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 ...

  6. Leetcode 24——Swap Nodes in Pairs

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

  7. (链表 递归) 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 ...

  8. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  9. [LeetCode] 24. Swap Nodes in Pairs ☆

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

随机推荐

  1. tomcat最大线程数的设置(转)

    1.Tomcat的server.xml中连接器设置如下 <Connector port="8080" maxThreads="150" minSpareT ...

  2. Hbase Region Server 启动失败

    错误如下:Master rejected startup because clock is out of sync org.apache.hadoop.hbase.ClockOutOfSyncExce ...

  3. Unity3D Android手机开发环境配置

    Unity3D Android手机开发环境配置 Date:2014-01-01 07:09 1.配置eclipse环境:首先在官网下载安装包:http://developer.android.com/ ...

  4. IOS 物理引擎

    来自IOS7 by tutorials   下面是个人的一点翻译总结 1,首先在初始化方法李画一个方块 UIView* square = [[UIView alloc] initWithFrame: ...

  5. JAVA实例变量的初始化过程

    假设有这样一段代码: public class Cat { private String name; private int age; public String toString() { retur ...

  6. [转] 使用SQL脚本查看表空间使用率和使用dba_tablespace_usage_metrics视图的差别

    传统的SQL脚本查看表空间使用率,使用的关键视DBA_DATA_FILE和DBA_FREE_SPACE. Oracle 11g引入了DBA_TABLESPACE_USAGE_METRICS视图.其实, ...

  7. 套接字socket 的地址族和类型、工作原理、创建过程

    注:本分类下文章大多整理自<深入分析linux内核源代码>一书,另有参考其他一些资料如<linux内核完全剖析>.<linux c 编程一站式学习>等,只是为了更好 ...

  8. iperf

    iperf命令是一个网络性能测试工具.iperf可以测试TCP和UDP带宽质量.iperf可以测量最大TCP带宽,具有多种参数和UDP特性.iperf可以报告带宽,延迟抖动和数据包丢失.利用iperf ...

  9. raphael入门到精通---属性和事件篇

    属性的使用 上一篇文章我们介绍了raphael如何生成基本的图形(元素),对于每个元素来讲,我们可以添加很多的元素(attr) 下面我就来简单的介绍下元素属性的使用(path元素属性我后面单独列出来介 ...

  10. 新建一个类并绑定一个activity

    1.新建一个类(.java 文件),继承Android.app.Activity 2.新建一个activity 文件 3.重写onCreate 方法,设置绑定activity 文件 @Override ...