LeetCode:24. Swap Nodes in Pairs(Medium)
1. 原题链接
https://leetcode.com/problems/swap-nodes-in-pairs/description/
2. 题目要求
给定一个链表,交换相邻的两个结点。已经交换的结点,不再进行交换。
注意:所使用的空间大小固定
例如,1->2->3->4转换后为2->1->4->3
3. 题目思路
使用一个遍历指针current和两个辅助指针first、second,first保存current指针所在结点的后继结点,second保存current指针所在结点的后继的后继结点。
令first的后继指向second的后继,current的后继等于second,current后继的后继等于first,current等于first
4. 代码实现
public class SwapNodeInPairs24 {
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(2);
ListNode l3 = new ListNode(3);
ListNode l4 = new ListNode(4);
ListNode l5 = new ListNode(5);
ListNode l6 = new ListNode(6);
l1.next = l2;
l2.next = l3;
l3.next = l4;
l4.next = l5;
l5.next = l6;
ListNode ls1 = l1;
while (ls1 != null) {
System.out.print(ls1.val);
ls1 = ls1.next;
}
System.out.println("");
ListNode ls2 = swapPairs(l1);
while (ls2 != null) {
System.out.print(ls2.val);
ls2 = ls2.next;
}
}
public static ListNode swapPairs(ListNode head) {
ListNode headPointer = new ListNode(0);
headPointer.next = head;
ListNode current = headPointer;
while (current.next != null && current.next.next != null) {
ListNode first = current.next;
ListNode second = current.next.next;
first.next = second.next;
current.next = second;
current.next.next = first;
current = first;
}
return headPointer.next;
}
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
}
LeetCode:24. Swap Nodes in Pairs(Medium)的更多相关文章
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【LeetCode】24. Swap Nodes in Pairs (3 solutions)
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- LeetCode 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- LeetCode 24. Swap Nodes in Pairs(交换链表中每两个相邻节点)
题意:交换链表中每两个相邻节点,不能修改节点的val值. 分析:递归.如果以第三个结点为头结点的链表已经两两交换完毕(这一步递归实现---swapPairs(head -> next -> ...
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- LeetCode OJ 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【leetcode】Swap Nodes in Pairs (middle)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- leetcode 之Swap Nodes in Pairs(21)
不允许通过值来交换,在更新指针时需要小心. ListNode *swapNodes(ListNode* head) { ListNode dummy(-); dummy.next = head; fo ...
随机推荐
- Android(java)学习笔记28:泛型概述和基本使用
1. 泛型的概述和基本使用: package cn.itcast_01; import java.util.ArrayList; import java.util.Iterator; /* * Arr ...
- 「Luogu-U18201」分析矿洞
题目 没有看懂题目呢说的是什么,但是我们要求的是这个式子 \[Ans=\sum_{i=1}^n\sum_{j=1}^n\varphi(gcd^2(i,j))\] 看起来挺鬼畜的是吧 老方法枚举\(gc ...
- 【luogu P3369 普通平衡树(Treap/SBT)】 模板 Splay
题目链接:https://www.luogu.org/problemnew/show/P3369 #include <cstdio> #include <algorithm> ...
- 菜鸟笔记 -- Chapter 6.2.2 标识符
6.2.2 标识符 Java中使用标识符来作为类.方法.字段的名称,在Java基础中我们已经简单了解过标识符的定义方法和驼峰命名.本节我们来研究一下标识符的长度问题,难道类名.方法名都可以无限长吗? ...
- 嵌入式:FreeRTOS的使用(未完)
为了方便与UCOS对比,顺序按照UCOS那篇编写. 0.一些移植.系统相关 1.框架写法(个人习惯相关) 1-1.main 函数里创建一个开始任务 int main(void) { 初始化外设 xTa ...
- Java研究
Strap 箱线图 峰度 随机过程 马尔科夫 超几何分布 贝叶斯公式 随机变量 德摩根 功率谱 残差 吸收壁 平稳随机 chorst 深入JVM OSGI ...
- MVVM、MVC框架的认识
推荐博客: https://blog.csdn.net/jia12216/article/details/55520426 https://www.cnblogs.com/sunny_z/p/7093 ...
- 关于Mobius反演
欧拉函数 \(\varphi\) \(\varphi(n)=\)表示不超过 \(n\) 且与 \(n\) 互质的正整数的个数 \[\varphi(n)=n\cdot \prod_{i=1}^{s}(1 ...
- 2018 Wannafly summer camp Day3--Shopping
Shopping 描述 题目描述: 你要买n件物品,其中有一些是凳子. 商场正在举行促销活动,如果购物车中有至少一个凳子,那么你可以半价购买这个购物车中最贵的一个物品. 你有m辆购物车,请最小化你的花 ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--A-跳台阶
链接:https://www.nowcoder.com/acm/contest/90/A 来源:牛客网 1.题目描述 小明在坐景驰科技研发的无人车到达了目的地. 景驰科技(JingChi.ai)是一家 ...