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.

题解:

  没什么好写的,链表题一般要注意头结点的问题,还有一定要画图!

Solution 1

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* swapPairs(ListNode* head) {
  12. ListNode dummy(-);
  13. dummy.next = head;
  14. ListNode* cur = &dummy;
  15.  
  16. while (cur->next && cur->next->next) {
  17. ListNode* tmp = cur->next->next;
  18. cur->next->next = tmp->next;
  19. tmp->next = cur->next;
  20. cur->next = tmp;
  21. cur = tmp->next;
  22. }
  23.  
  24. return dummy.next;
  25. }
  26. };

  递归:

Solution 2

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* swapPairs(ListNode* head) {
  12. if (!head || !head->next)
  13. return head;
  14. ListNode* newHead = head->next;
  15. head->next = swapPairs(head->next->next);
  16. newHead->next = head;
  17. return newHead;
  18. }
  19. };

【LeetCode】024. Swap Nodes in Pairs的更多相关文章

  1. 【LeetCode】24. Swap Nodes in Pairs (3 solutions)

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

  2. 【LeetCode】24. Swap Nodes in Pairs

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

  3. 【一天一道LeetCode】#24. Swap Nodes in Pairs

    一天一道LeetCode系列 (一)题目 Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  4. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  5. LeetCode 024 Swap Nodes in Pairs

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

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

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

  7. Leetcode 线性表 Swap Nodes in Pairs

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

  8. LeetCode解题报告—— Swap Nodes in Pairs & Divide Two Integers & Next Permutation

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

  9. [LeetCode 题解]:Swap Nodes in Pairs

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a li ...

随机推荐

  1. PopupWindowFromBottom 从底部弹出popupwindow

    自定义PopupWindowFromBottom public class PopupWindowFromBottom extends PopupWindow { public PopupWindow ...

  2. Jaxb2 实现JavaBean与xml互转

    一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...

  3. PBR探索

    原理 根据能量守恒,以及一系列光照原理得出微表面BRDF(Bidirectional Reflectance Distribution Function)公式 // D(h) F(v,h) G(l,v ...

  4. 梯度下降算法(gradient descent)

    简述 梯度下降法又被称为最速下降法(Steepest descend method),其理论基础是梯度的概念.梯度与方向导数的关系为:梯度的方向与取得最大方向导数值的方向一致,而梯度的模就是函数在该点 ...

  5. Python 面试题(上)

    Python语言特性 1 Python的函数参数传递 看两个例子: a = 1 deffun(a): a = 2 fun(a) printa # 1 a = [] deffun(a): a.appen ...

  6. ICCV 2015 B-CNN细粒度分类

    哈哈,好久没写博客了....最近懒癌发作~~主要是因为心情不太好啊,做什么事情都不太顺心,不过已经过去啦.最近一直忙着公司的项目,想用这个网络,就给大家带来了的这篇文章.可能比较老,来自ICCV 20 ...

  7. MyBatis:学习笔记(4)——动态SQL

    MyBatis:学习笔记(4)——动态SQL 如果使用JDBC或者其他框架,很多时候需要你根据需求手动拼装SQL语句,这是一件非常麻烦的事情.MyBatis提供了对SQL语句动态的组装能力,而且他只有 ...

  8. 用 Java 技术创建 RESTful Web 服务

    JAX-RS:一种更为简单.可移植性更好的替代方式 JAX-RS (JSR-311) 是一种 Java™ API,可使 Java Restful 服务的开发变得迅速而轻松.这个 API 提供了一种基于 ...

  9. 每天一个Linux命令(7)pwd命令

    pwd命令以绝对路径的方式显示用户当前工作目录.命令将当前目录的全路径名称(从根目录)写入标准输出.全部目录使用/分隔.第一个/表示根目录,最后一个目录是当前目录.     (1)用法介绍: pwd[ ...

  10. C#Winform之等待窗体

    窗体主要代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...