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.

 

head

dimmy -> 1 --> 2 --> 3 --> 4  对cur先后再前

pre       cur

class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == NULL)
return head;
ListNode *dummy = new ListNode(0);
dummy->next = head;
ListNode *pre = dummy, *cur=head;
while(cur&&cur->next)
{
pre->next = cur->next;
cur->next = cur->next->next;
pre->next->next = cur; pre = cur;
cur = cur->next;
}
return dummy->next; }
};

  

leetcode 024的更多相关文章

  1. Java for LeetCode 024 Swap Nodes in Pairs

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

  2. LeetCode 024 Swap Nodes in Pairs 交换链表中相邻的两个节点

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

  3. LeetCode 024 Swap Nodes in Pairs

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

  4. 【LeetCode】024. 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 Animation 题目图解汇总(持续更新中...)

    我会尽力将LeetCode上所有的题目都用动画的形式演示出来,期待与你见证这一天! GitHub Repo:LeetCode Animation Follow: MisterBooo · GitHub ...

  6. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  9. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

随机推荐

  1. QueryOver<T>

    NHibernate 数据查询之QueryOver<T>   一.限制运算符 Where:筛选序列中的项目WhereNot:反筛选序列中的项目 二.投影运算符 Select:创建部分序列的 ...

  2. JavaScript 动画库和开发框架

    1. Tween JS TweenJS 是一个简单的 JavaScript 补间动画库.能够很好的和 EaselJS 库集成,但也不依赖或特定于它.它支持渐变的数字对象属性和 CSS 样式属性.API ...

  3. 传说中的WCF(1):这东西难学吗?

    WCF难学吗? 是啊,这问题估计很多人都会问,也包括阿拉在内,也有此深刻而严重的凝问. 也有人说:“如何某项技术可以化繁为简,学起来轻松一点就好了.”也许,人类开生就摆脱不了一种习性——懒惰:不过,也 ...

  4. messageBox以及新窗体的使用

    DialogResult dr = MessageBox.Show("你确定要点击吗", "安全提示", MessageBoxButtons.YesNo);   ...

  5. c#通用多线程基类,以队列形式

    c#通用多线程基类,以队列形式 个人原创.欢迎转载.转载请注明出处.http://www.cnblogs.com/zetee/p/3487084.html 多线程这个概念大家都很熟悉,对于winfor ...

  6. Getting Started with Core Data

    Getting Started with Core Data Getting Started with Core Data Coreframework支持创建对象模型封装你的应用数据和逻辑满足MVC设 ...

  7. 企业架构研究总结(43)——企业架构与建模之ArchiMate详述(下)

    2.7 关系模型元素 企业架构模型包括了各种概念元素以及他们之间的关系,这其中的概念元素已经在前面几节中进行了阐述,而这些概念元素之间的关系则是本节的叙述重点.虽然ArchiMate中具有种类繁多的概 ...

  8. Google 域名的秘密

    声明:本文采用 BY-NC-SA 协议进行授权. 转载请注明转自:Google 域名的秘密 哈哈,我也标题党一回. 其实也不算是秘密,只能说是我在制作 https://github.com/lenny ...

  9. JS错误:Uncaught SyntaxError: Unexpected token ILLEGAL

    $('tbody', '#' + tableId).append('<tr onmouseover="this.style.backgroundColor=\'#eeeeee\'&qu ...

  10. linux memory release commands内存清理/释放命令

    linux 内存清理/释放命令 You could find reference from here: http://jingyan.baidu.com/article/597a06436a687f3 ...