leetcode-algorithms-24 Swap Nodes in Pairs
leetcode-algorithms-24 Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
Note:
Your algorithm should use only constant extra space.
You may not modify the values in the list's nodes, only nodes itself may be changed.
解法
一个链表指针指向链表的首地址,这个用来改变node的值,且将指针后移.
再申明两个链表指针,其中一个指针b是另个指针a的下个值(b = a->next).
现在循环处理.
/**
* 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 **l = &head;
ListNode *a = nullptr;
ListNode *b = nullptr;
while((a = *l) && (b = a->next))
{
a->next = b->next;
b->next = a;
*l = b;
l = &(a->next);
}
return head;
}
};
时间复杂度: O(n).
空间复杂度: O(1).
leetcode-algorithms-24 Swap Nodes in Pairs的更多相关文章
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【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 ...
- 【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- 【LeetCode】24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- LeetCode OJ 24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- LeetCode:24. Swap Nodes in Pairs(Medium)
1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 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 Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
随机推荐
- 【C#】可空类型 NullAble<T>
在实际编写代码时候 , 会遇到很多场景, 需要将值置成空, 比如发货日期, 有可能是没有. 在没有可空类型之前, 程序都是用 魔值, 即为一个minValue或者常量, 来代表这个值为空, 也有用一 ...
- Kubernetes之Controllers一
ReplicaSet is the next-generation Replication Controller. The only difference between a ReplicaSet a ...
- java泛型的作用和好处
转载于:https://blog.csdn.net/u012760183/article/details/52092692 之前参加面试的时候遇到面试官问泛型的作用,只是说了大概自己的理解, 在此转载 ...
- TortoiseGit自动记住用户名密码的方法
TortoiseGit自动记住用户名密码的方法 windows下比较比较好用的git客户端有2种: msysgit + TortoiseGit(乌龟git) GitHub for Windows gi ...
- 使用 jenkins 搭建CI/CD流水线 (MAC)
如何搭建持续集成/持续交付平台?? 如何使用jenkins搭建持续交付流水线,以及和其他工具(如artifactory)集成?如何使用元数据,记录软件发布过程的构建信息,测试结果,并用rest Api ...
- urllib模块中的方法
urllib模块中的方法 1.urllib.urlopen(url[,data[,proxies]]) 打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作.本例试着打开google ...
- 理解ffmpeg中的pts,dts,time_base
首先介绍下概念: PTS:Presentation Time Stamp.PTS主要用于度量解码后的视频帧什么时候被显示出来 DTS:Decode Time Stamp.DTS主要是标识读入内存中的b ...
- C/C++.文件是否存在
1. 2._access, _waccess.html(https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx) int _access( con ...
- HTML5语义化
转载自:https://www.cnblogs.com/fliu/articles/5244866.html 1.什么是HTML语义化? 用合理.正确的标签来展示内容,比如h1~h6定义标题,便于开发 ...
- 学习笔记45—Linux压缩集
1.压缩功能 安装 sudo apt-get install rar 卸载 sudo apt-get remove rar 2.解压功能 安装 sudo apt-get install unrar 卸 ...