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. 核心思想:百度网盘怎么盈利(互联网的高速更新决定了:亏钱你还有点机会,放弃连门都进不了),战略预备队 good

    百度做网盘很大程度就是为了防止别人依靠网盘做大和积累点技术储备.腾讯邮箱怎么赚钱?腾讯影音怎么赚钱?互联网的高速更新决定了,一些你看不起眼的软件很可能就会席卷整个市场,所以互联网大佬宁愿一些项目亏钱也 ...

  2. 关于java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream解决办法

    吉林的一个项目有个错误找了一天,有段报错是:   java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream 1.遇到过两次,第 ...

  3. FileObverse文件观察者的Debug报告

    FileObverse文件观察者的Debug报告 2014年9月18日 9:03

  4. 22.allegro中PCB打印设置[原创]

    1. -- 2. 3. 4. ----

  5. WC约束示使用

    1.接口中添加时要注意Xml序列化标签不要随意添加啊.[webInvoke].[OpertorContract]这2个约束就行了.不然,直接坑死啊.

  6. NDK(16)Jni中GetStaticFieldID和GetMethodID 中的类型标识串

    env在GetStaticFieldID和GetMethodID 时,函数参数和返回值的类型要指定类型标识串,如: jmethodID init = env->GetMethodID(clz,& ...

  7. oracle .bash_profile

    [oracle@redhat4 ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashr ...

  8. HDU 3467 (求五个圆相交面积) Song of the Siren

    还没开始写题解我就已经内牛满面了,从晚饭搞到现在,WA得我都快哭了呢 题意: 在DotA中,你现在1V5,但是你的英雄有一个半径为r的眩晕技能,已知敌方五个英雄的坐标,问能否将该技能投放到一个合适的位 ...

  9. Jqgrid demo-史上最强大,没有之一

            为了大家能够更好的学习和使用Jqgrid网格插件,我决定用Strtus2+Spring+hibernate+Jquery+Jqgrid实现一个Jqgrid网格插件的demo.当然官方网 ...

  10. C#委托的介绍(delegate、Action、Func、predicate)【转】

    转自 http://www.cnblogs.com/akwwl/p/3232679.html 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1 ...