题意:

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. CUDA ---- 简介

    CUDA简介 CUDA是并行计算的平台和类C编程模型,我们能很容易的实现并行算法,就像写C代码一样.只要配备的NVIDIA GPU,就可以在许多设备上运行你的并行程序,无论是台式机.笔记本抑或平板电脑 ...

  2. 检查 CPU 是否支持二级地址转换 - 摘自网络

    Windows 8 Consumer Preview 于2月正式发布,随后 Windows Server 8 Beta 也公布了下载.整体对比,Windows 8 在硬件方面的要求并不高,其最低硬件需 ...

  3. html5 canvas 移动小方块

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. 门户级UGC系统的技术进化路线——新浪新闻评论系统的架构演进和经验总结(转)

    add by zhj:先收藏了 摘要:评论系统是所有门户网站的核心标准服务组件之一.本文作者曾负责新浪网评论系统多年,这套系统不仅服务于门户新闻业务,还包括调查.投票等产品,经历了从单机到多机再到集群 ...

  5. ESB后台error日志

    本地tomcat没异常 开发环境,生产环境 ESB使用axis2.jar 后台会有错误,但不影响所有流程,该错误源自common.log的error,在捕捉异常后,并未往外继续抛 [ESB 打印] 接 ...

  6. maven 部署到tomcat 的 resource问题

    1.maven resource结构 如图,我将resoures下建立四个子文件夹,base存放的是不随环境变化的配置项,而其他三个均是对应环境的配置文件. 2.问题 我执行maven命令是没有的,但 ...

  7. spring mvc中的valid

    当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作: 声明Validator的方式: 1.为每一个Controller声明一个Validator @Controller publi ...

  8. Array types are now written with the brackets around the element type

    因为网站翻译的时候应该用的beta/beta2,而再beta4中就会出现问题,解决问题方案: var shopping: String[] = ["Eggs","Milk ...

  9. UVaLive 6694 Toy Boxes (二分+想法)

    题意:给出n个数,把n个数放在三个盒子里,每个盒子里的数绑在一起,要拿出来任何一个数的时候,所承担的重量是整个盒子的总重量,求最小总重量和. 析:感觉吧,就是轻的放的多一些,拿的次数多一些,大的放的少 ...

  10. (博弈论)hdoj 1079 Calendar Game

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1079 题解:题目大意,两个人Adam和Eve一块儿玩游戏,游戏规则是从1900年1月1日到2001年1 ...