[刷题] 24 Swap Nodes in Paris
要求
- 给定一个链表,对于每两个相邻的节点,交换其位置
示例
- 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的更多相关文章
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 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 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- 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 ...
- 【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 ...
- 24. Swap Nodes in Pairs 链表每2个点翻转一次
[抄题]: Given a linked list, swap every two adjacent nodes and return its head. Example: Given 1->2 ...
- [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 ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
随机推荐
- PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...
- C++并发与多线程学习笔记--线程之间调度
condition_variable wait() notify_one notify_all condition_variable 条件变量的实际用途: 比如有两个线程A和B,在线程A中等待一个条件 ...
- 不想eject,还咋修改create-react-app的配置?
一.先抛问题 许多刚开始接触create-react-app框架的同学,不免都会有个疑问:如何在不执行eject操作的同时,修改create-react-app的配置.今天胡哥就来带大家一起来看看这个 ...
- django+x-admin管理后台模板开发管理后台案例(设计部分)
使用django+x-admin管理后台模板搭建管理后台 一.环境需求 1.django:3.1 2.python:3.7 3.x-admin:2.2 4.pycharm:2020.3.2 5.ubu ...
- 201871030118-雷云云 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告
项目 内容 课程班级博客 班级链接 这个作业要求链接 作业链接 我的课程学习目标 (1)体验软件项目开发中的两人合作,练习结对编程(2)掌握Github协作开发程序的操作方法(3)学习遗传算法 这个作 ...
- 自学转行JAVA,没有项目经历怎么找工作?
应届生或者是刚参加工作的转行人员都有这样一个疑惑,刚学出来没有工作经验,但是企业又要求你必须要有工作经验,但是刚毕业找不到工作就不可能有工作经验,感觉陷入一个死循环.其实这种情况那些企业是不可能不知道 ...
- Ionic5沉浸式状态栏 适配全面屏
1. 在platforms/android/app/src/main目录中找到AndroidManifest.xml文件,修改文件中manifest → application → activity标 ...
- 针对中国政府机构的准APT攻击样本Power Shell的ShellCode分析
本文链接网址:http://blog.csdn.net/qq1084283172/article/details/45690529 一.事件回放 网络管理员在服务器上通过网络监控软件检测到,有程序在不 ...
- Android NDK编程之Android.mk和Application.mk
Android编程使用NDK必须创建一个jni文件夹,并且jni文件里一般包含有C/C++的源码文件.Android..mk文件.Application.mk文件(可选),Android.mk文件的编 ...
- 路由器逆向分析------QEMU的下载和安装(Linux平台)
本文博客地址:http://blog.csdn.net/qq1084283172/article/details/68953160 一.QEMU源码的下载和编译 QEMU源码的github下载地址:h ...