【一天一道LeetCode】#24. Swap Nodes in Pairs
一天一道LeetCode系列
(一)题目
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
(二)解题
给定一个链表,交换相邻两个节点,这道题要特别注意越界问题。
评级easy的题!就不多废话了。
/**
* 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) {
if(head == NULL) return NULL;
ListNode* p = head;
ListNode* pnext = head->next;
while(p!=NULL&&pnext!=NULL)
{
int tmp = pnext->val;
pnext->val = p->val;
p->val = tmp;
if(pnext->next !=NULL) p = pnext->next; //考虑越界问题,如[1,2,3,4]
else p=NULL;
if(p!= NULL && p->next != NULL) pnext = p->next;//考虑越界问题,如[1,2,3,4,5]
else pnext=NULL;
}
return head;
}
};
【一天一道LeetCode】#24. Swap Nodes in Pairs的更多相关文章
- 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 ...
- [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 ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
- Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
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
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交换节点对
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 (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
随机推荐
- 初识在Spring Boot中使用JPA
前面关于Spring Boot的文章已经介绍了很多了,但是一直都没有涉及到数据库的操作问题,数据库操作当然也是我们在开发中无法回避的问题,那么今天我们就来看看Spring Boot给我们提供了哪些疯狂 ...
- springMVC源码分析--HandlerAdapter(一)
HandlerAdapter的功能实际就是执行我们的具体的Controller.Servlet或者HttpRequestHandler中的方法. 类结构如下:
- linux 最大文件描述符
Linux对应用程序能打开的的最大文件描述符数量有两个层次的限制:用户级限制和系统级限制. 用户级限制是指目标用户运行的所有进程总共能打开的文件描述符数. 系统级的限制是指所有用户总共能打开的文件描述 ...
- HDFS:NameNode、DataNode、SecondaryNameNode
可以一句话描述 HDFS:把客户端的大文件存放在很多节点的数据块中. HDFS设计原则: 1,文件以块(block)方式存储: 2,通过副本机制提高可靠度和读取吞吐量: 3,每个区块至少分到三台Dat ...
- Linux目录架构详解
Linux和Windows操作系统的显著区别之一就是目录架构的不同.Linux操作系统的目录架构遵循文件系统层级结构标准.不知你是否使用ls命令浏览过Linux的根目录"/",亲爱 ...
- 18 Loader代码案例
目录结构: MainActivity.java 代码: package com.qf.day18_loader_demo2; import android.app.Activity; import a ...
- 【java虚拟机系列】从java虚拟机字节码执行引擎的执行过程来彻底理解java的多态性
我们知道面向对象语言的三大特点之一就是多态性,而java作为一种面向对象的语言,自然也满足多态性,我们也知道java中的多态包括重载与重写,我们也知道在C++中动态多态是通过虚函数来实现的,而虚函数是 ...
- Axure实现淡入淡出效果
小伙伴们有可能在各大网站看到淡入淡出效果的动画,比如淘宝.京东,淘宝每天会把各种打折促销.今日推荐.限时抢购等做成淡入淡入或者向右活动等类似翻页的效果放在首页,吸引顾客的眼球,那么如何使用Axure来 ...
- JEECG&Dubbo Demo
jeecg框架service与dao层类图 吴水成Dubbo Demo项目架构图
- android PakageManagerService启动流程分析
PakageManagerService的启动流程图 1.PakageManagerService概述 PakageManagerService是android系统中一个核心的服务,它负责系统中Pac ...