题目

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;
}
};

GitHub測试程序源代码

LeetCode(24) Swap Nodes in Pairs的更多相关文章

  1. LeetCode之旅(21)-Swap Nodes in Pairs

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  2. 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 ...

  3. 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 ...

  4. leetcode第23题--Swap Nodes in Pairs

    Problem: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1 ...

  5. 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. ...

  6. LeetCode(24)-Balanced Binary Tree

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  7. LeetCode(24): 两两交换链表中的节点

    Medium! 题目描述: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说 ...

  8. 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 ...

  9. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

随机推荐

  1. [Bayesian] “我是bayesian我怕谁”系列 - Variational Autoencoders

    本是neural network的内容,但偏偏有个variational打头,那就聊聊.涉及的内容可能比较杂,但终归会 end with VAE. 各个概念的详细解释请点击推荐的链接,本文只是重在理清 ...

  2. jQuery选择器(子元素过滤选择器)第七节

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  3. Problem J: 求个最大值

    Problem J: 求个最大值 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 871  Solved: 663[Submit][Status][Web ...

  4. 通讯框架 T-io 学习——给初学者的Demo:ShowCase设计分析

    前言 最近闲暇时间研究Springboot,正好需要用到即时通讯部分了,虽然springboot 有websocket,但是我还是看中了 t-io框架.看了部分源代码和示例,先把helloworld敲 ...

  5. Knowledge_SPA——精研查找算法

    首先保证这一篇分析查找算法的文章,气质与大部分搜索引擎搜索到的文章不同,主要体现在代码上面,会更加高级,会结合到很多之前研究过的内容,例如设计模式,泛型等.这也与我的上一篇面向程序员编程--精研排序算 ...

  6. css实现左侧固定宽,右侧自适应的7中方法

    一个面试会问的问题,如何实现两个盒子,左侧固定宽度,右侧自适应. 1.利用 calc 计算宽度的方法 css代码如下: .box{overflow: hidden;height: 100px;marg ...

  7. 阿里云VPS搭建Hexo博客

    最近买了一个阿里云服务器,准备写自己的网站,和将自己的作品放在上面:开始的时候,感觉就一个服务器应该很简单,但是从申请域名到备案,再到服务器搭建,没想到一波三折:闲话不多说,只是记录我在搭建时,最简单 ...

  8. mysql数据库的安装与基本配置

    目录 绿色版下载 mysql绿色版(5.7版本的安装与配置) 绿色版下载: mysql官网下载地址:https://www.oracle.com/index.html mysql绿色版(5.7版本的安 ...

  9. Gitlab自动触发Jenkins构建打包

    一.目的 在部门的测试环境中,开发人员一旦向gitlab仓库提交成功代码,gitlab就会自动触发jenkins构建项目.当然在构建后还可以添加项目部署或者自动化测试的脚本.这里只针对测试环境. 二. ...

  10. Mysql语句查询优化

    其实对Mysql查询语句进行优化是一件非常有必要的事情. 如何查看当前sql语句的执行效率呢? 1.建一张学生表 CREATE TABLE `student` ( `stu_id` ) NOT NUL ...