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. python os模块一些常用操作

    os.getcwd() ## 获取当前路径 os.chdir("dirpath") ## 改变目录 os.makedirs("dirname") ## 递归创建 ...

  2. oracle修改某字段不是必输性

    执行sql: ALTER TABLE table_name MODIFY 要修改的字段名字 NULL;

  3. ES6 利用 Set 数组去重法

    例子: const set = new Set(); [2, 3, 5, 4, 5, 2, 2].forEach(x => set.add(x) ); const arr = [...set]; ...

  4. java并发编程与高并发解决方案

    下面是我对java并发编程与高并发解决方案的学习总结: 1.并发编程的基础 2.线程安全—可见性和有序性 3.线程安全—原子性 4.安全发布对象—单例模式 5.不可变对象 6.线程封闭 7.线程不安全 ...

  5. 从页面到服务器,node实现文件下载

    起因: 新来了一个需求,让用户下载一个200m的zip文件,并且校验用户信息,难点:下载的文件是200M的. 现在维护的系统,以前的文件下载,走的是node的静态文件,用的express框架上自带的静 ...

  6. 从0开始 数据结构 AC自动机 hdu 2222

    参考博客 失配指针原理 使当前字符失配时跳转到另一段从root开始每一个字符都与当前已匹配字符段某一个后缀完全相同且长度最大的位置继续匹配,如同KMP算法一样,AC自动机在匹配时如果当前字符串匹配失败 ...

  7. 分析ReentrantLock的实现原理

    http://www.jianshu.com/p/fe027772e156 什么是AQS AQS即是AbstractQueuedSynchronizer,一个用来构建锁和同步工具的框架,包括常用的Re ...

  8. 理解多线程管理类 CWorkQueue

    有些人会觉得多线程无非是,有多少任务就启动多少线程,CreadThread,执行完了自己结束就释放资源了,其实不然.多线程是需要管理的,线程的启动.执行.等待和结束都需要管理,线程间如何通信,如何共享 ...

  9. 关于javascript以及jquery如何打开文件

    其实很简单, <input type="file" id="file" mce_style="display:none"> 这个 ...

  10. Jenkins的安装和使用

    1.可以参考W3C----https://www.w3cschool.cn/jenkins/jenkins-5h3228n2.html 两种方式安装Jenkins a.安装包 b.Jenkins.wa ...