要求

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

示例

  • 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. 前端 | JS Promise:axios 请求结果后面的 .then() 是什么意思?

    Promise 是JS中一种处理异步操作的机制,在现在的前端代码中使用频率很高.Promise 这个词可能有点眼生,但你肯定见过 axios.get(...).then(res => {...} ...

  2. K8S单集群桌面安装笔记【k8s-for-docker-desktop】

    一.K8S集群基本的拓扑结构 二.下载 k8s-for-docker-desktop k8s桌面单集群安装,基本上选择 k8s-for-docker-desktop或者minikube两类,本文采用前 ...

  3. kong 结合 istio demo

  4. flexbox(弹性盒布局模型),以及适用场景

    一.是什么 Flexible Box 简称 flex,意为"弹性布局",可以简便.完整.响应式地实现各种页面布局 采用Flex布局的元素,称为flex容器container 它的所 ...

  5. Dynamics CRM实体系列之窗体

    本节开始讲Dynamics CRM的窗体排版和设计,窗体也就是我们实际可以看到的表单界面.Dynamics CRM提供了一套独立的表单模板设计引擎,可以很方便的为开发者提供无代码开发,只需要简单的拖动 ...

  6. 【剑指offer】8:跳台阶

    题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 解题思路: 这种题目多为找规律求通用公式并最终用代码实现. 首先,考 ...

  7. JavaWeb 补充(Json)

    HTML DOM alert() 方法 定义和用法 alert() 方法用于显示带有一条指定消息和一个 OK 按钮的警告框. 参数 描述 message 要在 window 上弹出的对话框中显示的纯文 ...

  8. xctf - forgot

    xctf - forgot check一下,开启了NX 拉入ida中,能找到: __isoc99_scanf,能够无限输入, 循环中,读取32个scanf的字符并进行判断,最后根据结果调用存在栈上的函 ...

  9. 20 行简单实现一个 unstated-next 🎅

    前言 unstated-next 基于 React 心智模型(hook+context)而设计的状态管理. 在 react hook 出现之前,有基于单一数据源,使用纯函数修改状态的 redux &a ...

  10. 8-50.Pow(x,n)

    题目描述: 解题思路: 第一想法是递归,结果f(x,n) = x * f(x,n-1);这种方法的空间复杂度太高了,太想当然. 看了下题解:采取分治的方法:f(x,n) = f(x,n/2) * f( ...