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;
}
};

最新文章

  1. 发布自己的pods到CocoaPods trunk 及问题记录
  2. ASP.NET Core 开发 - Entity Framework (EF) Core
  3. 20145233韩昊辰 《Java程序设计》实验报告一:Java开发环境的熟悉(Windows+IDEA)
  4. HADOOP :: java.lang.ClassNotFoundException: WordCount
  5. python学习第七天
  6. Solr4.8.0源码分析(1)之Solr的Servlet
  7. POJ2632 Crashing Robots(模拟)
  8. Android编程 获取网络连接状态 及调用网络配置界面
  9. C# 制作Java +Mysql+Tomcat 环境安装程序,一键式安装
  10. c++的输入和输出流
  11. Java:reflection
  12. Java学习笔记之——Set容器
  13. 用JS解决url地址中参数乱码的问题
  14. MySQL 存储过程参数
  15. C语言 矩阵的转置及矩阵的乘法
  16. vue使用element Transfer 穿梭框实现ajax请求数据和自定义查询
  17. java泛型中<?>和<T>区别
  18. JS实现自定义排序
  19. Redmine 安装指南
  20. MySQL多线程备份工具mydumper

热门文章

  1. gulp之压缩图片
  2. POJ2513(字典树+图的连通性判断)
  3. python的WeakKeyDictionary类和weakref模块的其他函数
  4. shell入门-shell特性
  5. ueditor1.4.3jsp版在上传图片报"未找到上传文件"解决方案
  6. Selenium并行启动多个浏览器
  7. 使用JFileChooser保存文件
  8. 使用showOptionDialog显示多项选择框
  9. 基本算法思想之递推算法思想(C++语言描述)
  10. Material使用09 MdCheckboxModule、MdMenuModule、MdTooltipModule