一天一道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. makefile 和shell文件相互调用

    shell 文件内调用makefile文件:   #!/bin/bash cd ctemplate-2.1./configuresudo make -f installcd ../cd Templat ...

  2. Scikit-learn:scikit-learn快速教程及实例

    http://blog.csdn.net/pipisorry/article/details/52251305 :7] y = dataset[:,8] 我们将在下面所有的例子里使用这个数据组,换言之 ...

  3. SQLite 语法(http://www.w3cschool.cc/sqlite/sqlite-syntax.html)

    SQLite 语法 SQLite 是遵循一套独特的称为语法的规则和准则.本教程列出了所有基本的 SQLite 语法,向您提供了一个 SQLite 快速入门. 大小写敏感性 有个重要的点值得注意,SQL ...

  4. JSP 2.x 自定义标签

    JSP 1.x的标签,虽然使用起来非常灵活,但是比较复杂,JSP 2.x提供了一组简化的标签写法 SimpleTagSupport是SimpleTag接口的子类,同时支持参数和标签体,最核心的方法时d ...

  5. 非负矩阵分解NMF

    http://blog.csdn.net/pipisorry/article/details/52098864 非负矩阵分解(NMF,Non-negative matrix factorization ...

  6. 开源负载均衡通讯分发器(LB dispatcher) - G5

    from:http://bbs.csdn.net/topics/390753043 1.开发背景今天和系统运维的老大聊天,谈到一直在用的F5,行里对其评价为价格过高.功能复杂难懂,反正印象不是很好,使 ...

  7. C语言与java语言中数据类型的差别总结

    在学习java的时候,看到char ch =  '男' ; 我就觉得很奇怪,char类型不是占用一个字节吗?为什么定义成一个汉字被说成是一个字符了? 原来,在C语言中,char在32位操作系统下占用1 ...

  8. 2.关于QT中数据库操作,简单数据库连接操作,数据库的增删改查,QSqlTableModel和QTableView,事务操作,关于QItemDelegate 代理

     Linux下的qt安装,命令时:sudoapt-get install qt-sdk 安装mysql数据库,安装方法参考博客:http://blog.csdn.net/tototuzuoquan ...

  9. 手机微博(weibo.cn)模拟登录及页面解析

    package com.laudandjolynn.test; import java.io.IOException; import java.io.OutputStream; import java ...

  10. [ExtJS5学习笔记]第二节 Sencha Cmd 学习笔记 使你的sencha cmd跑起来

    本文地址: http://blog.csdn.net/sushengmiyan/article/details/38313537 本文作者:sushengmiyan ----------------- ...