[刷题] 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 ...
随机推荐
- 死磕Spring之AOP篇 - Spring AOP常见面试题
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- Dynamics CRM实体系列之字段
本节开始讲实体中的基础数据存储对象,也就是字段. Dynamics CRM目前总共有13种字段类型,分别为单行文本.选项集.多选选项集.两个选项.图像.整数.浮点数.十进制数.货币.多行文本.日期和时 ...
- windows上phpstudy配置memcache
原文 http://blog.csdn.net/ltx06/article/details/78588448 总的来说,分两步:同时安装memcached软件服务和安装php_memcache ...
- Toolkit 大更新:UI 更美观,用起来更方便!
前言 前段时间有小伙伴在群里聊天,说到 Toolkit 下载量到 4.9k 了.就突然想起来,很久没有更新这个插件. PS:我是用它申请了 License,一般时候使用 Json 格式化功能. 趁着周 ...
- Ambassador-06-金丝雀部署
金丝雀部署:Canary Releases ambassador使用加权循环,在多个服务之间路由流量.收集所有服务的完整指标,以便比较canary和production的相对性能.这个weight的值 ...
- selenium启动IE失败,并报错:Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones
1.selenium去启动IE时,报错: Started InternetExplorerDriver server (32-bit)2.50.0.0Listening on port 24641On ...
- k8s新手必看
转载自https://juejin.im/post/6844903840139968520 Kubernetes零基础快速入门!初学者必看! 起源 Kubernetes 源自于 google 内部的服 ...
- JavaScript 通过身份证号获取出生日期、年龄、性别 、籍贯
JavaScript 通过身份证号获取出生日期.年龄.性别 .籍贯(很全) 效果图: 示例代码: //由于没有写外部JS,所以代码比较长!!! <!DOCTYPE html PUBLIC &qu ...
- 进程保护原理Hook函数Openprocess
Win32子系统: ...
- hdu4530 水题
题意: 小Q系列故事--大笨钟 Time Limit: 600/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...