题目

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. 【Shell脚本学习24】Shell输入输出重定向:Shell Here Document,/dev/null文件

    Unix 命令默认从标准输入设备(stdin)获取输入,将结果输出到标准输出设备(stdout)显示.一般情况下,标准输入设备就是键盘,标准输出设备就是终端,即显示器. 输出重定向 命令的输出不仅可以 ...

  2. 使用腾讯IP分享计划网站中的纯JS省市区三级联动

    JS地址:http://ip.qq.com/js/geo.js 实例如下: <!DOCTYPE html> <html> <head> <title>省 ...

  3. iOS开发 - 在状态栏显示FPS,CPU和内存信息

    原理 FPS的计算 CoreAnimation有一个很好用的类CADisplayLink,这个类会在每一帧绘制之前调用,并且可以获取时间戳.于是,我们只要统计出,在1s内的帧数即可. - (void) ...

  4. 使用后台程序的第一个表单Form

    参考手册:http://www.yiichina.com/doc/guide/2.0/start-forms 1.创建模型:advanced\backend\models\moxing.php 此模型 ...

  5. JS事件阻止冒泡的写法

    $("body").on("click", "#id", function (ev) { ev = ev || event;要写的逻辑代码 ...

  6. 在Spark集群中,集群的节点个数、RDD分区个数、​cpu内核个数三者与并行度的关系

    梳理一下Spark中关于并发度涉及的几个概念File,Block,Split,Task,Partition,RDD以及节点数.Executor数.core数目的关系. 输入可能以多个文件的形式存储在H ...

  7. json代码驾照考题批量加入MySQL数据库 ps.executeUpdate()永远只能悲催的加一条数据 去掉id主键自增 用foreach循环数据库只能出现一条语句

    package com.swift; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStrea ...

  8. vue学习之路 - 2.基本操作(上)

    基本操作(上) 本章节简介: vue的安装 vue实例创建 数据绑定渲染 表单数据双向绑定 事件处理 安装 安装方式有三种: 一.vue官网直接下载 http://vuejs.org/js/vue.m ...

  9. GNU C中__attribute__

    __attribute__基本介绍: 1. __attribute__ 可以设置函数属性.变量属性和类型属性. 2. __attribute__ 语法格式为:__attribute__ ((attri ...

  10. elasticsearch 大量数据翻页到后面无数据解决

    默认情况下报错信息:from + size 不能大于10000 {"error":{"root_cause":[{"type":" ...