Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list's nodes, only nodes itself may be changed.

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.

题意:

给定一个链表,每两个节点做一下交换。

Solution1: Two Pointers(fast & slow) 

(1) use Two Pointers(fast & slow)  to find a pair

(2)swap

(3) move forward to find a new pair

code:

 /*
Time: O(n)
Space: O(1)
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
head = dummy;
while (head.next != null && head.next.next != null) {
// narrow to find a pair
ListNode slow = head.next;
ListNode fast = head.next.next; // swap
head.next = fast;
slow.next = fast.next;
fast.next = slow; // move to next pair
head = slow;
}
return dummy.next;
}
}

[leetcode]24. Swap Nodes in Pairs交换节点对的更多相关文章

  1. 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 ...

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

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

  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. Java [leetcode 24]Swap Nodes in Pairs

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

  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. selenium 网络请求

    selenium 网络请求 browser.find_element_by_id("id的name")browser.find_element("")brows ...

  2. Dynamics 365 CRM large instance copy

    使用CRM 大家想必都做过copy. 从一个instance 复制到另外一个instance. 如果你是Dynamics 365 CRM 用户, 并且你的instance超过500GB,甚至1TB+的 ...

  3. 域名到站点的负载均衡技术一览(主要是探讨一台Nginx抵御大并发的解决方案)(转)https://www.cnblogs.com/EasonJim/p/7823410.html

    一.问题域 Nginx.LVS.Keepalived.F5.DNS轮询,往往讨论的是接入层的这样几个问题: 1)可用性:任何一台机器挂了,服务受不受影响 2)扩展性:能否通过增加机器,扩充系统的性能 ...

  4. centos 使用RPM包安装指定版本的docker-engine

    下面是拿安装docker-engine-1.10.3-1为例: wget https://yum.dockerproject.org/repo/main/centos/7/Packages/docke ...

  5. zip()函数,max()和min(),built-in function,import模块,read(),readlines(),write(),writelines(),with..as..文件处理方式

    zip()函数:将可迭代对象作为参数,将对象中的对应元素打包成一个个元组. #map()普通的输出例子 print(list(zip(('a','n','c'),(1,2,3)))) print(li ...

  6. PCL中Sample_consensus分割支持的几何模型

    随机采样一致分割法,从点云中分割出线.面等几何元素 #include <pcl/sample_consensus/method_types.h> #include <pcl/samp ...

  7. 搜狗浏览器总是打开123.sogou.com-记搜狗浏览器遭遇劫持一例

    昨日,因从网上下载了office2010破解工具,压缩包中有个文件为名为[office 2010激活工具\为保证永久激活,要先点击这个配置,再点击KMSELDIYI激活.exe],单击之后没有反应.后 ...

  8. 读取Excel的部分问题

    1.office分很多版本,导致Excel连接字符串不同. 2.是否有标题头的问题(在连接字符串中设置) 3.Excel本身删除分数据删除和表格结构删除.普通delete只能删除数据, 还是能读取到表 ...

  9. WPF Binding Mode,UpdateSourceTrigger

    WPF 绑定模式(mode) 枚举值有5个1:OneWay(源变就更新目标属性)2:TwoWay(源变就更新目标并且目标变就更新源)3:OneTime(只根据源来设置目标,以后都不会变)4:OneWa ...

  10. Tomcat 配置详解和优化

    2018年01月09日 18:14:41 tianxiaojun2014 阅读数:306   转自:https://www.cnblogs.com/xbq8080/p/6417671.html htt ...