一天一道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的更多相关文章

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

  2. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

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

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

  4. Java [leetcode 24]Swap Nodes in Pairs

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

  5. Leetcode 24——Swap Nodes in Pairs

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

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

  7. (链表 递归) 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 ...

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

  9. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  10. [LeetCode] 24. Swap Nodes in Pairs ☆

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

随机推荐

  1. 自定义一个仿Spinner

    两个布局文件: adpter_list.xml <?xml version="1.0" encoding="utf-8"?> <LinearL ...

  2. GitHub Android Librarys Top 100 简介

    GitHub Android Librarys Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据GitHub搜索J ...

  3. java 随机数高效生成

    分享牛,分享牛原创.近期去面试经常被问到java如何生产随机数,以及生成很大的字符串保证不能重复,还要考虑性能,之前本人面试别人的时候,可能不会问这个问题.既然这个java随机数问题经常被问到,那咱们 ...

  4. java创建线程

    创建一个线程 Java提供了两种创建线程方法: 通过实现Runable接口: http://blog.csdn.net/duruiqi_fx/article/details/52187275 通过继承 ...

  5. Leetcode解题-链表(2.2.3)PartitionList

    题目:2.2.3 Partition List Given a linked list and a value x, partition it such that all nodes less tha ...

  6. Dynamics CRM2016 Web API之创建记录

    前篇介绍了通过primary key来查询记录,那query的知识点里面还有很多需要学习的,这个有待后面挖掘,本篇来简单介绍下用web api的创建记录. 直接上代码,这里的entity的属性我列了几 ...

  7. OpenCV+python 人脸识别

    首先给大家推荐一本书:机器学习算法原理与编程实践 本文内容全部转载于书中,相当于一个读书笔记了吧 绪论 1992年麻省理工学院通过实验对比了基于结构特征的方法与基于模版匹配的方法,发现模版匹配的方法要 ...

  8. ubuntu挂载的NTFS文件编译失败问题

    错误: 编译Android源代码时候出现,权限拒绝的错误 解决方法: sudo apt-get install ntfs-config sudo ntfs-config 我的微信二维码如下,欢迎交流讨 ...

  9. 2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程

     1  Lucen目录介绍 2  lucene-core-3.6.2.jar是lucene开发核心jar包 contrib  目录存放,包含一些扩展jar包 3  案例 建立第一个Lucene项目 ...

  10. 18 UI美化自定义形状shape

    自定义某个控件的形状 如 圆角 巨型 环形 : 在工程文件的新建 res/drawable/shape文件(以下键一个圆角) <?xml version="1.0" enco ...