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.

如题实例所示,需要成对的交换节点,这里我用到了两个帮助节点,一个记下起点的前面位置,一个作为每一对prev的前面一个节点使用,思路比较简单,就是交换之后再向后面移动两个节点就可以了,代码如下所示:

 /**
* 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 NULL;
if(!head->next) return head;
ListNode * helper1 = new ListNode(INT_MIN);
ListNode * helper2 = new ListNode(INT_MIN);
helper1->next = head;
helper2->next = head->next;// 先记录下首节点的位置,供函数返回的时候使用
ListNode * prev = head;
ListNode * curr = head->next;
while(prev){
if(curr){
ListNode * tmpNode = curr->next;
curr->next = prev;
helper1->next = curr;
prev->next = tmpNode;
helper1 = prev;
if(prev->next && prev->next->next){
prev = prev->next;
curr = prev->next;
}else
break;
}else
break; }
return helper2->next;
}
};

LeetCode OJ:Swap Nodes in Pairs(成对交换节点)的更多相关文章

  1. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  2. LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  3. [LintCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head.   Example Given 1->2-> ...

  4. [LeetCode] Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  5. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  6. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  7. 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 ...

  8. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  9. LeetCode 024 Swap Nodes in Pairs

    题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  10. [Leetcode] Swap nodes in pairs 成对交换结点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...

随机推荐

  1. 小练手:用HTML5 Canvas绘制谢尔宾斯基三角形

    文章首发于我的知乎专栏,原地址:https://zhuanlan.zhihu.com/p/26606208 以前看到过一个问题:谢尔宾斯基三角形能用编程写出来么?该怎么写? - 知乎,在回答里,各方大 ...

  2. JVM(3) 垃圾回收器与内存分配策略

    文章内容摘自:深入理解java虚拟机 第三章   对象已死? 1. 引用计数算法: 给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器为0 ...

  3. vue下载文件

    import fileDownload from 'js-file-download' let params = { ", ", "filename":&quo ...

  4. JS对象深入剖析

    对象概述 Objects are mutable keyed collections.  An object is a container of properties, where a propert ...

  5. vs LNK2019 无法解析的外部符号 ***,该符号在函数 WinMain 中被引用

    一般链接错误都是因为包含头文件与lib库不匹配(无导出函数.lib库的release debug版本混乱.库引用的优先级.编译器设置mt/mtd等等)造成的. 错误    LNK2019    无法解 ...

  6. 20145324 《Java程序设计》第1周学习总结

    20145324 <Java程序设计>第1周学习总结 教材学习内容总结 1.Java是程序语言.标准规范.代表解决问题的平台 2.三大平台:Java SE(JVM.JRE.JDK与Java ...

  7. MyBatis如何返回自增的ID

    <insert id="insertTable" parameterType="com.infohold.city.map.model.CheckTemplateM ...

  8. getJson同步

    $.ajaxSettings.async = false;//在执行之前加$.ajaxSettings.async = false;  (同步执行)  function get_no_order_ar ...

  9. Spring Boot CRUD+分页(基于Mybatis注解方式)

    步骤一:关于Mybatis Mybatis 是用来进行数据库操作的框架.其中分页使用Mybatis中的PageHelper插件. Mybatis与hibernate对比: 1.hibernate是一个 ...

  10. hadoop系统的端口

    hadoop系统部署时用到不少端口.有的是Web UI所使用的,有的是内部通信所使用的,有的是监控所使用的.实际系统中可能用于防火墙的端口设计.一些内部通信用的端口可能也需要外部能访问.如两个集群的数 ...