LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点
2024-09-03 21:29:00
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(!head)
return head;
ListNode* dummy=new ListNode(-),*pre=dummy;
dummy->next=head;
while(pre->next&&pre->next->next)
{
ListNode* last=pre->next->next;
pre->next->next=last->next;
last->next=pre->next;
pre->next=last;
pre=last->next;
}
return dummy->next;
}
};
最新文章
- 发布自己的pods到CocoaPods trunk 及问题记录
- ASP.NET Core 开发 - Entity Framework (EF) Core
- 20145233韩昊辰 《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)
- HADOOP :: java.lang.ClassNotFoundException: WordCount
- python学习第七天
- Solr4.8.0源码分析(1)之Solr的Servlet
- POJ2632 Crashing Robots(模拟)
- Android编程 获取网络连接状态 及调用网络配置界面
- C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装
- c++的输入和输出流
- Java:reflection
- Java学习笔记之——Set容器
- 用JS解决url地址中参数乱码的问题
- MySQL 存储过程参数
- C语言 矩阵的转置及矩阵的乘法
- vue使用element Transfer 穿梭框实现ajax请求数据和自定义查询
- java泛型中<;?>;和<;T>;区别
- JS实现自定义排序
- Redmine 安装指南
- MySQL多线程备份工具mydumper
热门文章
- gulp之压缩图片
- POJ2513(字典树+图的连通性判断)
- python的WeakKeyDictionary类和weakref模块的其他函数
- shell入门-shell特性
- ueditor1.4.3jsp版在上传图片报";未找到上传文件";解决方案
- Selenium并行启动多个浏览器
- 使用JFileChooser保存文件
- 使用showOptionDialog显示多项选择框
- 基本算法思想之递推算法思想(C++语言描述)
- Material使用09 MdCheckboxModule、MdMenuModule、MdTooltipModule