【LeetCode OJ】Swap Nodes in Pairs
题目: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.
代码:
struct ListNode
{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {} };
ListNode *swapPairs(ListNode *head)
{
if (head == NULL || head->next == NULL)
return head;
ListNode *p = head;
ListNode *q = p->next;
while (q)
{
int temp;
temp = p->val;
p->val = q->val;
q->val = temp;
if (q->next != NULL)
{
p = q->next;
if (p->next != NULL)
q = p->next;
else
break; }
else
break;
}
return head;
}
【LeetCode OJ】Swap Nodes in Pairs的更多相关文章
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 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 OJ:Swap Nodes in Pairs(成对交换节点)
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- [LeetCode 题解]:Swap Nodes in Pairs
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- Leetcode 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
- LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation
1. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For e ...
随机推荐
- (笔记)Linux下查看CPU使用率的命令
1.top 使用权限:所有使用者 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示process的动态 d :改变显示的更新速度,或 ...
- 第三百八十七节,Django+Xadmin打造上线标准的在线教育平台—网站上传资源的配置与显示
第三百八十七节,Django+Xadmin打造上线标准的在线教育平台—网站上传资源的配置与显示 首先了解一下static静态文件与上传资源的区别,static静态文件里面一般防止的我们网站样式的文件, ...
- Java如何使用重载方法处理异常?
在Java编程中,如何使用重载方法处理异常? 此示例显示如何使用重载方法来处理异常.需要在每个方法中使用try catch块. package com.yiibai; public class Exc ...
- SQL Server CLR 使用 C# 自定义函数
一.简介 Microsoft SQL Server 2005之后,实现了对 Microsoft .NET Framework 的公共语言运行时(CLR)的集成.CLR 集成使得现在可以使用 .NET ...
- MongoDB多文档查询
db.getCollection('transactionCompensation').find( { "$and":[ { "status":{ " ...
- (实用)win7/8修改远程桌面连接默认端口
记录备忘. 在启用windows操作系统的远程连接时,使用默认的3389端口是一件比较危险的事情,通常我们将其改成一个比较独特的端口,使得目标系统不会直接将远程桌面连接的功能直接暴露在网络环境下. 步 ...
- C# 将DateTime.Now.DayOfWeek的值转为中文星期
1. 如果常规使用DateTime.Now.DayOfWeek则得到是英文,如果加ToString后再加上相关参数(“G”.“g”.“X”.“x”.“F”.“f”.“D”或“d”),则还会有所变化. ...
- R语言colorRampPalette函数-创建颜色梯度(渐变色)
在绘热图时,需要将数值映射到不同的颜色上,这时就需要一系列的颜色梯度 colorRampPalette 函数支持自定义的创建一系列的颜色梯度 代码示例: > colors <- color ...
- C# 反射(GetType) 获取动态Json对象属性值的方法
之前在开发一个程序,希望能够通过属性名称读取出属性值,但是由于那时候不熟悉反射,所以并没有找到合适的方法,做了不少的重复性工作啊! 然后今天我再上网找了找,被我找到了,跟大家分享一下. 其实原理并不复 ...
- Linux环境下Redis安装配置步骤[转]
在LInux下安装Redis的步骤如下: 1.首先下载一个Redis安装包,官网下载地址为:https://redis.io/ 2.在Linux下解压redis: tar -zxvf redis-2. ...