要求

  • 给定一个链表,对于每两个相邻的节点,交换其位置

示例

  • 1->2->3->4->NULL
  • 2->1->4->3->NULL

实现

 1 struct ListNode {
2 int val;
3 ListNode *next;
4 ListNode(int x) : val(x), next(NULL) {}
5 };
6
7 class Solution {
8 public:
9 ListNode* swapPairs(ListNode* head) {
10 ListNode* dummyHead = new ListNode(0);
11 dummyHead->next = head;
12
13 ListNode* p = dummyHead;
14 while( p->next && p->next->next ){
15 ListNode* node1 = p->next;
16 ListNode* node2 = node1->next;
17 ListNode* next = node2->next;
18
19 node2->next = node1;
20 node1->next = next;
21 p->next = node2;
22
23 p = node1;
24 }
25
26 ListNode* retNode = dummyHead->next;
27 delete dummyHead;
28
29 return retNode;
30 }
31 };

相关

  • 25 Reverse Nodes in k-Group
  • 147 Insertion Sort List
  • 148 Sort List

[刷题] 24 Swap Nodes in Paris的更多相关文章

  1. 24. Swap Nodes in Pairs

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

  2. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  3. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

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

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

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

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

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

  7. 24. Swap Nodes in Pairs 链表每2个点翻转一次

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

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

  9. 【一天一道LeetCode】#24. Swap Nodes in Pairs

    一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...

随机推荐

  1. 第一次OOP作业-Blog总结

    前言 第一次作业一共八道题,此次作业也是这三次作业中最接近面向过程程序设计的题目集,整体难度偏低,总耗时1.5h,主要的知识点在熟悉Java的语法上,整体题目的逻辑非常清晰简单,但最后一个判断三角形类 ...

  2. 20182217_刘洪宇 后门原理与实践 EXP2

    1.后门概念 后门就是不经过正常认证流程而访问系统的通道. 哪里有后门呢? 编译器留后门 操作系统留后门 最常见的当然还是应用程序中留后门 还有就是潜伏于操作系统中或伪装为特定应用的专用后门程序. - ...

  3. Github 镜像资源

    1.GitHub 镜像访问 这里提供两个最常用的镜像地址(别登录账号): https://github.com.cnpmjs.org https://hub.fastgit.org 也就是说上面的镜像 ...

  4. 云计算和AI时代,运维应该如何做好转型?

    云计算和AI时代,运维应该如何做好转型? 今天我们来聊一聊,在云计算和AI时代,运维应该如何做好转型?今天的内容可以说是我们前面运维组织架构和协作模式转型的姊妹篇.针对运维转型这个话题,谈谈我的思考和 ...

  5. Database | 浅谈Query Optimization (2)

    为什么选择左深连接树 对于n个表的连接,数量为卡特兰数,近似\(4^n\),因此为了减少枚举空间,早期的优化器仅考虑左深连接树,将数量减少为\(n!\) 但为什么是左深连接树,而不是其他样式呢? 如果 ...

  6. Python基础(十五):Python的3种字符串格式化,做个超全对比!

    有时候,为了更方便.灵活的运用字符串.在Python中,正好有3种方式,支持格式化字符串的输出 . 3种字符串格式化工具的简单介绍 python2.5版本之前,我们使用的是老式字符串格式化输出%s. ...

  7. BUAA_OO_2020_第二单元总结

    BUAA_OO_2020_第二单元总结 第一次 设计策略 本次作业采用生产者.消费者模式设计,大致框架如图所示: 生产者:输入线程 消费者:电梯线程 托盘:Dispatcher调度器 线程安全方面,调 ...

  8. (四)Struts2的Action(深入讲解版)

    Struts2的Action 开发者需要提供大量的Action,并在struts.xml中配置Action.Action类里包含了对用户请求的处理逻辑,因为我们也称Action为业务控制器. 一.编写 ...

  9. 现代 CLI 和 GUI 方案指南

    原文链接:http://axuebin.com/articles/fe-solution/cli/desc.html,转载请联系 写在前面 提到初始化项目,那就必须提到脚手架这个名词,那为什么这篇文章 ...

  10. Spring Boot demo系列(一):Hello World

    2021.2.24 更新 1 新建工程 打开IDEA选择新建工程并选择Spring Initializer: 可以在Project JDK处选择JDK版本,下一步是选择包名,语言,构建工具以及打包工具 ...