[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-& ...
随机推荐
- PythonStudy——函数嵌套定义 Function nesting definition
# 在一个函数内部定义另一个函数 # 函数对象(变量)与普通对象(变量)一样,在函数内部定义,随函数调用而产生, # 调用结束而销毁,所以只能在函数内部调用 def outer(): print('o ...
- Keepalived+LVS实现高可用负载均衡双主模式
LVS是一种集群(Cluster)技术:采用IP负载均衡技术和基于内容请求分发技术.调度器具有很好的吞吐率,将请求均衡地转移到不同的服务器上执行,且调度器自动屏蔽掉服务器的故障,从而将一组服务器构成一 ...
- 三、tcp、ip协议详细
1. 什么是 TCP/IP? TCP/IP 是一类协议系统,它是用于网络通信的一套协议集合. 传统上来说 TCP/IP 被认为是一个四层协议 1) 网络接口层: 主要是指物理层次的一些接口,比如电缆等 ...
- linux查看进程启动的时间点
ps -ef |grep xxx # 先查找进程pid ps -wo pid,lstart -p {pid}
- C#编程经验-enum and struct
enum,store fixed values,use array replace,not use this data-structurestruct,store several variables, ...
- java中int算法的有趣现象
今天无意中发现一个怪事,当时没理解,后来跟网友讨论了才知道原理,是关于int值的加法算法,两段代码如下: 代码1: @Test public void test1() { ; ; try { whil ...
- [转]Java事件处理机制- 事件监听器的四种实现方式
原文来自http://stefan321.iteye.com/blog/345221 自身类作为事件监听器 外部类作为事件监听器 匿名内部类作为事件监听器 内部类作为事件监听器 自身类作为事件监听器: ...
- [UE4]Button
一.按钮有4种状态:Normal(普通状态).Hovered(鼠标悬停状态).Pressed(鼠标按下状态).Disabled(禁用状态),可以分别给每种状态设置样式. 二.按钮有如图所示的5个事件, ...
- Group Pathfinding & Movement in RTS Style Games
转自:http://gamasutra.com/blogs/AndrewErridge/20180522/318413/Group_Pathfinding__Movement_in_RTS_Style ...
- atop 分析小记
atop分析小记 atop这个工具相当NB 项目中需要用到它的磁盘使用率统计值,为了一探究竟,挖了下它的代码 atopsar atopsar实际就是atop的一个链接指向. 从atop.c的main源 ...