题目

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 || !(head->next) ) return head;
ListNode dummy(-);
dummy.next = head;
ListNode *prev = &dummy;
ListNode *curr = head;
while ( curr && curr->next )
{
prev->next = curr->next;
curr->next = curr->next->next;
prev->next->next = curr;
prev = curr;
curr = curr->next;
}
return dummy.next;
}
};

Tips:

链表基本操作,动手画图,直接出来。

===================================

第二次过这道题,一次AC了。

/**
* 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 dummpy(-);
dummpy.next = head;
ListNode* pre = &dummpy;
ListNode* curr = head;
while ( curr && curr->next )
{
pre->next = curr->next;
curr->next = curr->next->next;
pre->next->next = curr;
pre = curr;
curr = curr->next;
}
return dummpy.next;
}
};

【Swap Nodes in Pairs】cpp的更多相关文章

  1. leetcode 【 Swap Nodes in Pairs 】python 实现

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

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

  3. 【Reverse Nodes in k-Group】cpp

    题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...

  4. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  5. 【LeetCode】24. Swap Nodes in Pairs (3 solutions)

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

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

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

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

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

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

  9. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

随机推荐

  1. [转] Adobe acrobat 破解教程

    最新版的Adobe Acrobat DC Pro可以使我们更方便的管理和编辑PDF文档,现在我为大家带来Adobe Acrobat DC Pro安装及破解教程,供大家安装和使用. 工具/原料   Ad ...

  2. ArcGIS for Server新建站点异常,Failed to create the site.Failed to configure the server machine'XXXX',Server machine'XXXX' is not a local server machine.

      系统环境:操作系统Win7 64位,装在虚拟机VM中,ArcGIS for Server 10.2.1 问题描述:ArcGIS for Server 10.2.1安装并授权完成后,站点初始化时显示 ...

  3. javascript设计模式之装饰者模式

    /* * 装饰者模式提供比继承更有弹性的替代方案 * 在不改变原构造函数的情况下,添加新的属性或功能 */ //需要装饰的类(函数) function Macbook() { this.cost = ...

  4. GIT教程笔记

    GIT的工作流程: 先在工作目录中添加.修改文件 一般是在工作目录建立你的工程文件夹,然后通过命令行进入文件夹后  git init 初始化 将需要进行版本管理的文件放入缓存区  git add 文件 ...

  5. Java中的字符串问题

    本文章分为三个部分: 1.创建字符串对象的两种方式以及它们的存储方式 2.String a = new String("a")创建了几个对象的问题 3.字符串小例子 ------- ...

  6. eclipse Indigo搭建SSH框架详解

    SSH框架是最常用的框架之一,在搭建SSH框架的时候总有人遇到这样,那样的问题.下面我介绍一下SSH框架搭建的全过程. 第一步:准备工作.   下载好eclipse,Struts2,Spring,Hi ...

  7. IOS 线程描述

    ●什么是线程 ● 1个进程要想执行任务,必须得有线程(每1个进程至少要有1条线程) ● 线程是进程的基本执行单元,一个进程(程序)的所有任务都在线程中执行 ● 比如使用酷狗播放音乐.使用迅雷下载电影, ...

  8. POJ 3233 Matrix Power Series (矩阵分块,递推)

    矩阵乘法是可以分块的,而且幂的和也是具有线性的. 不难得到 Si = Si-1+A*Ai-1,Ai = A*Ai-1.然后矩阵快速幂就可以了. /*************************** ...

  9. 关于HTML(含HTML5)的块级元素和行级(内联)元素总结

    1.首先我们要知道什么是块级元素和行级(内联)元素? 块级(block)元素的特点: ①总是在新行上开始: ②高度,行高以及外边距和内边距都可控制: ③宽度缺省是它的容器的100%,除非设定一个宽度: ...

  10. ORA-04031: Unable To Allocate 32 Bytes Of Shared Memory

    记录一次生产库遇到的4031错误,后来通过调整sga大小将问题解决了 报错信息: ORA-04031: 无法分配 32 字节的共享内存 ("shared pool","s ...