LeetCode(24) 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.
分析
如演示样例所看到的。给定一个链表。要求交换链表中相邻两个节点。
对于此题的程序实现,必须注意的是指针的判空,否则。一不注意就会出现空指针异常。
AC代码
/**
* 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 || head->next == NULL)
return head;
ListNode *p = head , *q = p->next;
//首先交换头两个结点,同一时候保存q后结点
ListNode *r = q->next;
head = q;
p->next = r;
q->next = p;
if (r && r->next)
{
ListNode *pre = p;
p = p->next;
q = p->next;
while (q)
{
//保存q结点后结点
ListNode *r = q->next;
pre->next = q;
p->next = r;
q->next = p;
if (r && r->next)
{
pre = p;
p = r;
q = p->next;
}
else{
break;
}
}
}
return head;
}
};
LeetCode(24) Swap Nodes in Pairs的更多相关文章
- LeetCode之旅(21)-Swap Nodes in Pairs
题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...
- Leetcode 题目整理-6 Swap Nodes in Pairs & Remove Duplicates from Sorted Array
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现
题目: Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- leetcode第23题--Swap Nodes in Pairs
Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...
- LeetCode(25)Reverse Nodes in k-Group
题目 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- LeetCode(24)-Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode(24): 两两交换链表中的节点
Medium! 题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说 ...
- Leetcode-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 ...
随机推荐
- Linux系列教程(十五)——Linux用户和用户组管理之用户管理命令
上篇博客我们介绍了用户管理的相关配置文件,包括用户信息文件/etc/passwd,用户密码文件/etc/shadow:然后介绍了用户组信息文件/etc/group,用户组密码文件/etc/gshado ...
- Excel 生成SQL
'"&A21&"' Excel 中要做字符串连接 "& + 单元格地址 + &", 如果单纯做测试在某个单元格中测试输出内容 ...
- 3Sum探讨(Java)
探讨一下leetcode上的3Sum: Given an array S of n integers, are there elements a, b, c in S such that a + b ...
- Python连接Oracle数据库
今天使用Python连接数据库,连接没有问题,就是中文显示乱码,网上找了很多解决方案, 最后选择使用这个 #!/usr/bin/env python # -*- coding:utf-8 -*- #A ...
- 新一代的昆明网络seo优化技巧
一年一度的双11又即将到来,今天选择在双11这天新注册了一个博客园,第一篇文章,我决定来谈一谈现在的网络SEO. 起首咱们来熟悉下SEO是什么,SEO全名叫Search Engine Optimiza ...
- 前端面试题(5) 列举5种IE haslayout的属性及其值
haslayout 是Windows Internet Explorer渲染引擎的一个内部组成部分.在Internet Explorer中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父 ...
- JS小游戏:贪吃蛇(附源码)
javascript小游戏:贪吃蛇 此小游戏采用的是面向对象的思想,将蛇,食物,和游戏引擎分为3个对象来写的. 为方便下载,我把js写在了html中, 源码中暂时没有注释,等有空我在添加点注释吧. 游 ...
- [转]SQL Server 表变量和临时表的区别
一.表变量 表变量在SQL Server 2000中首次被引入.表变量的具体定义包括列定义,列名,数据类型和约束.而在表变量中可以使用的约束包括主键约束,唯一约束,NULL约束和CHECK约束(外键约 ...
- 69、django之Form组件
本篇导航: 小试牛刀 Form类 常用选择插件 自定义验证规则 初始化数据 Django的Form主要具有一下几大功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次 ...
- 如何完全根据官方下载包搭建hibernate框架
好久没有用s2sh的框架了,最近业务需要又要拾起来.在搭框架时,发现之前都是复制配置文件,对具体的细节却很懵懂,所以要从新来一遍,也是一次新的学习. 我使用的版本是hibernate-release- ...