[Linked List]Swap Nodes in Pairs
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) {
ListNode* first=head;
ListNode* second = first ? first->next:NULL;
ListNode* next=NULL,*pre=NULL;
while(first && second){
next = second->next;
second->next = first;
first->next = next;
pre ? pre->next = second: head = second;//有前驱的话,前驱的下一几点指向交换后的两个节点的头,否则,为第一次交换,更新头结点.
pre = first;
first=next;
second = first ? first->next:NULL;
}
return head;
}
};
[Linked List]Swap Nodes in Pairs的更多相关文章
- leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现
题目: Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- Leetcode-24 Swap Nodes in Pairs
#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [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 (双数交换节点) 解题思路和方法
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
随机推荐
- nodejs 计算内存使用率
//计算内存使用率 function calcMem(){ let mem_total = os.totalmem(), mem_free = os.freemem(), mem_used = mem ...
- WebApi 文件上传
1. 注意给form表单加上enctype = "multipart/form-data" 属性,否则会导致Action的参数HttpPostedFileBase 对象接收不到文件 ...
- VB.NET Shared(共享)和 Static(静态)关键字的区别
共享成员(Shared): VB.NET现在是支持真正的面向对象编程,可以继承.使用多态.共享成员 和静态成员. 共享成员就是在所有类和所定义派生类的实例之间共享的方法.属 性.字段和事件.所有使用类 ...
- node.js入门(三)调式
1.安装调式工具 打开命令行工具,输入以下内容,然后回车. npm install -g node-inspector 等待安装成功呢后,我们就可以使用 node-debug 文件名 这个命令来调式我 ...
- SQL Server 提高创建索引速度的 2 个方法
方法 1. 使用tempdb来提速 create index index_name on table_name (column_list) with(sort_in_tempdb = on); 方法 ...
- nginx请求体读取
上节说到nginx核心本身不会主动读取请求体,这个工作是交给请求处理阶段的模块来做,但是nginx核心提供了ngx_http_read_client_request_body()接口来读取请求体,另外 ...
- android 测量控件视图的方法
在实际项目中经常要用到 测量一个控件或者视图的高,宽.然后根据这个高宽进行一些逻辑. 计算视图宽高有几种方式先简单的了解下android 视图的绘制过程会促进理解. 一.android View绘制过 ...
- 新建Android工程没有自动生成R.JAVA,应该先升级下ADT
前几天非常郁闷,本来计划在Android上做个小东西,结果打开Eclipse新建工程,发现居然没有R.JAVA! 反复测试很多次,均未成功,最后试着升级了下ADT,结果搞定,在这里记下,下次遇到这样的 ...
- Geoserver基本使用、WMS服务发布与OpenLayers测试
1.Geoserver与OpenLayers的下载 Geoserver:http://geoserver.org/ OpenLayers:http://openlayers.org/ 2.安装部署Ge ...
- Eclipse被汉化后恢复EN模式
问题描述: 在安装Flush builder 的时候安装了汉化包,导致Eclipse中功能显示为汉字. 问题解决: 在Eclipse快捷方式下“目标”路径中添加-nl "EN"即可 ...