[leetcode]24. Swap Nodes in Pairs交换节点对
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:
Given1->2->3->4, you should return the list as2->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交换节点对的更多相关文章
- 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 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- [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 ...
- 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 ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
- Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- (链表 递归) 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 ...
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
随机推荐
- IDEA整合Junit测试框架
首先说一下为什么会有这篇文章吧,百度没有找到一个我想要的答案.也可以理解为,作为一个java菜鸟,一个对idea和jar理解不深的人,想跟着博客一步步操作完之后发现,哎不行,之后的牢骚吧.主要是为了记 ...
- expect脚本实现ssh自动登录
1:简单的实现ssh登录 #!/usr/bin/expect set ip "10.0.0.142" set user "root" set password ...
- webpack 中,importloaders 配置项的含义
importLoaders:用于配置「css-loader 作用于 @import 的资源之前」有多少个 loader. 0 => no loaders (default); 1 => p ...
- 日期date出参入参和timestamp转化
日期传到form页面,注意MM要大写 由于Date在MySQL没有默认值,没有设置的时候,会自动变成0000-00-00,影响渲染到Java类中,所以需要在程序中设置默认值 date转timestam ...
- Tomcat整体架构分析
下面让我们来看看Tomcat容器的整体结构: 本文的目的是覆盖这张图中所涉及的主要请求处理组件.而上图中的一些高级主题如集群和安全则不是在本文讨论的范围之内. 本图中,Service, Host, C ...
- 算法实践--不相交集合(Disjoint Sets)
什么是不相交集合(Disjoint Sets) 是这样的一组set,任何元素最多只能在一个set中 至少支持查找Find和合并Union操作 实现方式(基于树) 每个set都是一棵树 每棵树都由树的根 ...
- C/C++中的volatile简单描述
首先引入一篇博客: 1. 为什么用volatile? C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier.这是 BS 在 ...
- iframe-父子-兄弟页面相互传值(jq和js两种方法)
参考文章: http://blog.csdn.net/u013299635/article/details/78773207 http://www.cnblogs.com/xyicheng/archi ...
- Linux 入侵检测小结
Linux 入侵检测小结 0x00 审计命令 在linux中有5个用于审计的命令: last:这个命令可用于查看我们系统的成功登录.关机.重启等情况:这个命令就是将/var/log/wtmp文件格式 ...
- [ExcelHome]VLOOKUP的别样用法
请看题: 如上图所示,是某小区多名业主的信息表.如诸君所见,A列是业主的姓名,B列是一些有趣的信息,要求在C列,使用VLOOKUP函数,提取出B列的手机号码. B列的信息真是奇葩,除了手机号码,还有职 ...