当前位置:网站首页>力扣24-两两交换链表中的节点——链表
力扣24-两两交换链表中的节点——链表
2022-08-04 21:53:00 【张怼怼√】
题目描述
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
求解思路
建立一个虚拟节点指向head,辅助解题;
画图体会链表交换的过程,一切都能解释得通。
输入输出示例
代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode dump = new ListNode(0);
dump.next = head;
ListNode pre = dump;
while(pre.next != null && pre.next.next != null){
ListNode tem = head.next.next;
pre.next = head.next;
head.next.next = head;
head.next = tem;
pre = head;
head = head.next;
}
return dump.next;
}
}
边栏推荐
- 看看XDOC如何做Word文档预览
- OC-类簇
- What does Xinchuang mean?Which industries are involved?Why develop Xinchuang?
- LocalDate时间日期包的用法
- 数电快速入门(五)(编码器的介绍以及通用编码器74LS148和74LS147的介绍)
- docker 搭建mysql 主从复制
- "Jianzhi offer" brush title classification
- mysql基础
- 打卡第 2 天: urllib简记
- Open source summer | Cloud server ECS installs Mysql, JDK, RocketMQ
猜你喜欢
Chapter7 : Network-Driven Drug Discovery
[Linear Algebra 03] Elimination method display and 4 solutions of AX=b
Numpy on the superposition of two arrays
数电快速入门(三)(卡诺图化简法的介绍)
8 年产品经验,我总结了这些持续高效研发实践经验 · 协同篇
【线性代数03】消元法展示以及AX=b的4种解情况
数电快速入门(五)(编码器的介绍以及通用编码器74LS148和74LS147的介绍)
七夕特制:《牛郎会织女》
炽热如初 向新而生|ISC2022 HackingClub白帽峰会圆满举办!
如何一键重装Win11系统 一键重装系统方法
随机推荐
Flutter 实现背景图片毛玻璃效果
Domestic PMP certificate of gold content how
Milvus configuration related
The upgrade and transformation plan of the fortress machine for medium and large commercial banks!Must see!
大势所趋之下的nft拍卖,未来艺术品的新赋能
LeetCode 199: 二叉树的右视图
《剑指offer》刷题分类
rk3399-0.0 svc命令
VSCode - common shortcut keys (continuous recording
开发deepstram的自定义插件,使用gst-dseaxmple插件进行扩充,实现deepstream图像输出前的预处理,实现图像自定义绘制图(精四)
Axure9基本交互操作(一)
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd6 in position 120: invalid continuation byte
LeetCode143:重排链表
Yolov7:Trainable bag-of-freebies sets new state-of-the-art for real-time objectdetectors
docker 搭建mysql 主从复制
openresty lua-resty-template页面静态化
Develop your own text recognition application with Tesseract
第二讲 软件生命周期
【CC3200AI 实验教程 1】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-开发环境搭建
CountDownLatch使用及原理