题意:

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)

分析:

链表题,画个图就比较清晰了,就是每两个进行一下翻转,注意翻转部分头尾指针的指向即可,头指针会变,所以用下dummy node。

代码:

 /**
* 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) {
ListNode dummy();
dummy.next = head;
head = &dummy;
while (head -> next != nullptr && head -> next -> next != nullptr) {
ListNode* temp1 = head -> next;
head -> next = head -> next -> next;
ListNode* temp2 = head -> next -> next;
head -> next -> next = temp1;
temp1 -> next = temp2;
head = head -> next -> next;
}
return dummy.next;
}
};
 

LeetCode24 Swap Nodes in Pairs的更多相关文章

  1. Leetcode-24 Swap Nodes in Pairs

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

  2. Leetcode24.Swap Nodes in Pairs两两交换链表中的节点

    给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...

  3. 24. Swap Nodes in Pairs

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

  4. [LintCode] Swap Nodes in Pairs 成对交换节点

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

  5. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

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

  6. 【LeetCode练习题】Swap Nodes in Pairs

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

  7. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

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

  9. Leetcode 线性表 Swap Nodes in Pairs

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...

随机推荐

  1. jQuery和js如何判断checkbox是否选中

    jquery: <div id="divId" class="divTable"><div class="tableBody&quo ...

  2. LINQ标准查询操作符(二)——Join、GroupJoin、GroupBy、Concat、

    四.联接操作符 联接是指将一个数据源对象与另一个数据源对象进行关联或者联合的操作.这两个数据源对象通过一个共同的值或者属性进行关联. LINQ有两个联接操作符:Join和GroupJoin. 1. J ...

  3. 给Asp.net MVC Forms 验证设置角色访问控制

    当我们使用Asp.net MVC Forms方式验证用户, 然后设置Controller 或 Action 的 Authorize属性时, 默认情况下只有Users属性可以设置(这里的Users通常是 ...

  4. 第二百三十四天 how can I 坚持

    今天果然不负众望,下了一天的雪啊,挺好. 今天把花搞了下,都弄花盆里了,希望不会就这么挂掉.八千代,绿萝,还有小叶元宝. 中午喝了点酒,没感觉. 过两天气温就零下十多度了,该咋办啊,最怕冬天.家里现在 ...

  5. 【MySQL】查看MySQL配置文件路径及相关配置

    (1)关于配置文件路径 有时候,我发现虽然尝试修改了配置文件的一些变量,但是并没有生效.后来才发现原来是因为修改的文件并非MySQL服务器读取的配置文件. 如果不清楚MySQL当前使用的配置文件路径, ...

  6. .net 配置ueditor

    添加引用如下: <script src="../Ueditor/ueditor.config.js" type="text/javascript"> ...

  7. Gym 101064 D Black Hills golden jewels (二分)

    题目链接:http://codeforces.com/gym/101064/problem/D 问你两个数组合相加的第k大数是多少. 先sort数组,二分答案,然后判断其正确性(判断过程是枚举每个数然 ...

  8. CCF 201403-2 窗口 (STL模拟)

    问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的 ...

  9. Uncaught TypeError: Object #<Object> has no method 'fancybox'

    Uncaught TypeError: Object #<Object> has no method 'fancybox' 2011-10-24 16:51:19|  分类: html|举 ...

  10. json操作json类型转换

    前提是需要加Jar包: gson-2.2.2.jar package utils; import java.io.BufferedReader;import java.io.FileInputStre ...