LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)
题目标签:Linked List
题目给了我们一组 linked list,让我们把每对nodes 互换位置。
新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然后互换位置,具体看code。
Java Solution:
Runtime: 0 ms, faster than 100 %
Memory Usage: 34 MB, less than 100 %
完成日期:05/22/2019
关键点:换位置时候先后顺序
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode p = dummy; while(p.next != null && p.next.next != null) {
ListNode s1 = p.next;
ListNode s2 = p.next.next; // step 1: p -> s2
p.next = s2;
// step 2: s1 -> s2.next
s1.next = s2.next;
// step 3: s2 -> s1
s2.next = s1;
// step 4: p = s1
p = s1;
}
return dummy.next;
}
}
参考资料:LeetCode Discuss
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)的更多相关文章
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)
题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...
- 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两两交换链表中的节点
Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2->3 ...
- 24. Swap Nodes in Pairs[M]两两交换链表中的节点
题目 Given a linked list, swap every two adjacent nodes and return its head. You may not modify the va ...
- (链表 递归) 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 成对交换节点
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
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- 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 ...
随机推荐
- Delphi 窗体函数GetWindowRect 取窗口矩形坐标
GetWindowRect,用于取窗口矩形坐标.返回值类型:布尔型(LongBool).执行成功返回真(True),否则返回假(False);参数1类型:整数型(HWND),目标窗口的窗口句柄;参数2 ...
- Java类的成员之四:代码块.
3.2类的成员之四:代码块 ①初始化块(代码块)作用:对Java对象进行初始化 ②程序的执行顺序: ③一个类中初始化块若有修饰符,则只能被static修饰,称为静态代码块(static block ) ...
- DUBBO+Zookeeper在Centos7中本地搭建及小案例
环境: 1.centos7 2.jdk-7u76-linux-x64.tar.gz 2.tomcat:apache-tomcat-7.0.59.tar.gz 3.zookeeper-3.4.6.tar ...
- jsp+servlet中文乱码问题
jsp+servlet中文乱码问题 servlet想要获得前台传来的值 String strName=new String(request.getParameter("name") ...
- Java异常关闭资源的两种方式
try-catch-finally 常用,在异常关闭时应判断流是否为空 public class CloseableUtils { public static void closeable(Close ...
- 1060 Are They Equal (25 分)
1060 Are They Equal (25 分) If a machine can save only 3 significant digits, the float numbers 1230 ...
- 学习 Doug Lea 大神写的——Scalable IO in Java
学习 Doug Lea 大神写的--Scalable IO in Java 网络服务 Web services.分布式对象等等都具有相同的处理结构 Read request Decode reques ...
- C# WinFrom 关于MDI
dev是一个牛B 到没边的控件 我们正常用winform做个原始mdi窗体 一点都不好看 但 用的dev只需要一个控件 就可让显示舒服多了 建一个项目 上边放一个 xtraTabbedMdiManag ...
- 31-Ubuntu-用户权限-02-ls输出信息介绍
ls -l 查看文件夹下文件或目录的详细信息 1 2 3 4 5 6 7 8 9 10 d/- rwx rwx r-x 2 summmer summmer 12288 2月 25 13:34 Ente ...
- creat-react-app搭建的项目中按需引入antd以及配置Less和如何修改antd的主题色
在creat-react-app搭建的项目环境中按需引入antd以及配置less,首先需要暴露出来webpack文件.(此操作不可逆). create-react-app myapp 创建同一个rea ...