[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-& ...
随机推荐
- Day 07 字符编码,文件操作
今日内容 1.字符编码:人识别的语言与机器识别的语言转换的媒介 2.字符与字节:字符占多少字节,字符串转换 3.文件操作:操作硬盘的一块区域 字符编码 重点:什么是字符编码 人类能识别的字符等高级标识 ...
- docker基本命令使用
学会使用docker命令帮助 docker help 子命令 查看docker镜像 docker images 搜索镜像 docker search 镜像名 下载镜像 docker pull 镜像名 ...
- MySQL数据实时增量同步到Kafka - Flume
转载自:https://www.cnblogs.com/yucy/p/7845105.html MySQL数据实时增量同步到Kafka - Flume 写在前面的话 需求,将MySQL里的数据实时 ...
- CSV文件乱码展示(编码格式问题)
最开始mac上打开CSV文件乱码,是这样的:CSV文件编码格式为UTF-8 解决办法一:将excel文件同样的转换编码格式为utf-8,具体操作如下: 去掉tab,勾选comma 最后,将文件另存为u ...
- 关于offsetParent
不知道有多少人知道JavaScript中有offsetParent这么个属性. 关于offsetParent,我最开始见到他,是在<JavaScript高级程序设计(第3版)>第321页, ...
- Java 虚拟机面试题全面解析(干货)
Java 虚拟机面试题全面解析(干货) JDK 是什么 JRE 是什么 Java历史版本的特性 Java Version SE 50 Java Version SE 6 Java Version SE ...
- kettle 常用组件
Dummy步骤不会做任何事情.它的主要功能是作为以测试为目的的占位符. 追加流 分析查询(前后行查询),步骤:增加常量数据包括id,name连个字段,增加自增列autoid字段,按id,outid进行 ...
- JavaScript学习-2循环
文章目录 ----------①console函数 ----------②for循环 ----------③跳出循环 ----------④练习题:口诀表 ----------⑤练习题:幼兔 ---- ...
- TCC细读 - 2 核心实现
TCC,基于业务层面的事物定义,粒度完全由业务自己控制,本质上还是补偿的思路,它把事物运行过程分为try-confirm-cancel阶段,每个阶段逻辑由业务代码控制 业务活动管理器控制业务活动的一致 ...
- js:捕获冒泡和事件委托
一.事件流(捕获,冒泡) 事件流:指从页面中接收事件的顺序,有冒泡流和捕获流. 当页面中发生某种事件(比如鼠标点击,鼠标滑过等)时,毫无疑问子元素和父元素都会接收到该事件,可具体顺序是怎样的呢?冒 ...