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. java:synchronized

    synchronized:利用上锁实现数据同步,避免多线程操作的情况下,数据出现异常. 另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块. 一个线程获得了一个对象的同步锁,那这个对象上所 ...

  2. android从应用到驱动之—camera(2)---cameraHAL的实现

    本文是camera系列博客,上一篇是: android从应用到驱动之-camera(1)---程序调用流程 本来想用这一篇博客把cameraHAL的实现和流程都给写完的.搞了半天,东西实在是太多了.这 ...

  3. Case Study: Random Number Generation(翻译教材)

    很荣幸,经过三天的努力.终于把自己翻译的教材做完了,现在把它贴出来,希望能指出其中的不足.   Case Study: Random Number Generation Fig. 6.7  C++ 标 ...

  4. cocos2dx开发笔记

    1.帧动画:SpriteTest=>SpriteAnimationSplit 2.sourceinsight显示代码行 option->document option->editin ...

  5. apk反编译(4)Smali代码注入

    转自 : http://blog.sina.com.cn/s/blog_5674d18801019i89.html 应用场景 Smali代码注入只能应对函数级别的移植,对于类级别的移植是无能为力的.具 ...

  6. C#ShowCursor光标的显示与隐藏

      使用using System.Runtime.InteropServices; [DllImport("user32.dll" , EntryPoint = "Sho ...

  7. What's New for Visual C# 6.0

    https://msdn.microsoft.com/en-us/library/hh156499.aspx nameof You can get the unqualified string nam ...

  8. js二维码扫描

    Cordova 3.x 实用插件(2) -- 二维码Barcode : http://rensanning.iteye.com/blog/2034026 samples-camera: http:// ...

  9. 1651. Shortest Subchain(bfs)

    1651 终于A了 看这题容易想到最短路 看到错的很多 还特意注意了好几处 后来发现 必须按给出的顺序出边 想了想 这不就是BFS 然后就是各种细节 i->i+1ori->j(a[i]== ...

  10. Java [Leetcode 118]Pascal's Triangle

    题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...