1.题目描述

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.

2.解法分析

/**

 * 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) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        if(head==NULL)return NULL;

        

        ListNode *cur=head;

        

        //增加一个头结点有利于是代码整齐

        ListNode* myHead=new ListNode(-1);

        ListNode* prev=myHead;

        prev->next=cur;

        

        while(cur!=NULL&&cur->next!=NULL)

        {

            prev->next=cur->next;

            cur->next=cur->next->next;

            prev->next->next=cur;

            prev=cur;

            cur=cur->next;

        }

        

        prev=myHead->next;

        delete myHead;

        return prev;

        

        

    }

};

leetcode—Swap Nodes in Pairs的更多相关文章

  1. LeetCode: Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  2. [LeetCode]Swap Nodes in Pairs题解

    Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  3. [LeetCode] Swap Nodes in Pairs 成对交换节点

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

  4. Leetcode:Swap Nodes in Pairs 单链表相邻两节点逆置

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

  5. [LeetCode]Swap Nodes in Pairs 成对交换

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

  6. [Leetcode] Swap nodes in pairs 成对交换结点

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

  7. LeetCode Swap Nodes in Pairs 交换结点对(单链表)

    题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...

  8. leetcode Swap Nodes in Pairs python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  9. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

随机推荐

  1. QTP之delphi试用感想一(自动化测试)

    这两天一直在琢磨自动化测试,自动化测试,其实与单元测试有一些相同之处,单元测试的目的也是可以一次写,多次运行,对于测试驱动及后期维护真是有非常多的好处,用自动化测试工具也是如何,主要目的是为了回归测试 ...

  2. KMP算法的C++实现

    这个问题阮一峰老师讲的很清楚,链接 这里我只贴一下我的C++实现代码: #include <iostream> #include <cstring> #include < ...

  3. 如何创建支持Eclipse IDE的Maven项目

    使用Maven创建的项目是不支持任何IDE的,不能导入IDE中,因为项目格式都不符合特定IDE的格式要求,那么如何创建符合IDE要求的项目呢? 1.使用mvn eclipse:eclipse 命令把项 ...

  4. c 语言练习__求到N的阶乘的和。

    #include <stdio.h> /* 题目如下 * S = 1 + 2! + 3! + ... + N! */ int main(int argc, char *argv[]) { ...

  5. vs2012 arcgis engine 10 丢失arcgis模板

    1.Visual Studio 2012环境下安装ArcGIS Engine 10 Visual Studio 2012环境下安装ArcObject SDK for the Microsoft .Ne ...

  6. Eclipse中查看JDK源码设置

    设置方法如下: 1.路径 window-> Preferences -> Java -> Installed JRES 2.此时"Installed JRES"右 ...

  7. 基于XMPP的即时通信系统的建立(三)— 程序设计概览

    XMPP与HTTP的比较 XMPP的优势 Ÿ   1. 推送数据 HTTP只能从服务器哪里请求数据,除非服务器正在响应客户端请求,否则不能向客户端发送数据.但XMPP连接是双向的,任何一方在任何时候都 ...

  8. <pages validateRequest="false"/>在.net4.0中无效的问题

    再web.config中设置<pages validateRequest="false"/>在.net4.0中无效的问题 解决方案: <system.web> ...

  9. WebService教程和分析

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  10. [转:CSS3-前端] CSS3发光和多种图片处理

    原文链接:http://www.qianduan.net/css3-image-styles.html 一些上流的CSS3图片样式 神飞 发表于 24. Sep, 2011, 分类: CSS , 46 ...