leetcode-algorithms-24 Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

Example:

Given 1->2->3->4, you should return the list as 2->1->4->3.

Note:

Your algorithm should use only constant extra space.

You may not modify the values in the list's nodes, only nodes itself may be changed.

解法

一个链表指针指向链表的首地址,这个用来改变node的值,且将指针后移.

再申明两个链表指针,其中一个指针b是另个指针a的下个值(b = a->next).

现在循环处理.

/**
* 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 **l = &head;
ListNode *a = nullptr;
ListNode *b = nullptr;
while((a = *l) && (b = a->next))
{
a->next = b->next;
b->next = a;
*l = b;
l = &(a->next);
}
return head;
}
};

时间复杂度: O(n).

空间复杂度: O(1).

链接: leetcode-algorithms 目录

leetcode-algorithms-24 Swap Nodes in Pairs的更多相关文章

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

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

  2. 【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 ...

  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】24. Swap Nodes in Pairs

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

  5. LeetCode OJ 24. Swap Nodes in Pairs

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

  6. LeetCode:24. Swap Nodes in Pairs(Medium)

    1. 原题链接 https://leetcode.com/problems/swap-nodes-in-pairs/description/ 2. 题目要求 给定一个链表,交换相邻的两个结点.已经交换 ...

  7. 24. Swap Nodes in Pairs

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

  8. 24. Swap Nodes in Pairs(M);25. Reverse Nodes in k-Group(H)

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

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

  10. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

随机推荐

  1. Vue的生命周期(钩子函数)

    Vue一共有10个生命周期函数,我们可以利用这些函数在vue的每个阶段都进行操作数据或者改变内容. 其实在Vue的官网有一张图已经很好的诠释了生命周期,我在这里就不再多讲了,直接贴图,然后上程序代码. ...

  2. 【转载】C++宏定义详解

    摘自:http://blog.chinaunix.net/uid-21372424-id-119797.html   C++宏定义详解 2011-02-14 23:33:24   分类: C/C++ ...

  3. web开发测试注意点

    1.用户操作多页面情况 如果用session来获取当前页面情况时要特别注意,操作时出现另一个页面的情况,会出现传参数混乱 解决:后台可以获取并比对判断当前页面某些参数值

  4. 【Python】【异步IO】

    # [[异步IO]] # [协程] '''协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在 ...

  5. 【二十九】php之简易微信二维码支付

    参考二维码支付接口文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5 index.php <!DOCTYPE htm ...

  6. P1547 Out of Hay

    传送门     练习 只是一个最小生成树的水题,拿来练练模板 AC代码: #include<iostream> #include<cstdio> #include<alg ...

  7. 使用ajax无法跨源问题总结

    参考文章: 浏览器同源政策及其规避方法 跨域资源共享 CORS 详解 使用jQuery实现跨域提交表单数据 <form action="http://v.juhe.cn/weather ...

  8. Jenkins--Credentials添加证书从git上拉代码

    直接上图:

  9. 动态LINQ(Lambda表达式)

    1.准备数据实体 public class Data { public string AccountNO { get; set; } public int Count { get; set; } } ...

  10. rtrim

    <?php $str = '14岁'; $new_str = rtrim($str, '岁'); echo $new_str; 如果右边是'岁',就过滤掉.