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.

Subscribe to see which companies asked this question

 
未优化的代码,用3个临时指针
ListNode* swapPairs(ListNode* head) {
if (head == nullptr || head->next == nullptr)
return head;
ListNode *pre = head;
ListNode *cur = pre->next;
ListNode *net = cur->next;
head = cur;
while (true)
{
cur->next = pre;
if (net == nullptr || net->next == nullptr) {
pre->next = net;
break;
}
pre->next = net->next;
pre = net;
cur = pre->next;
net = cur->next;
}
return head;
}

惊人的解法,用双重指针,开阔思路,本以为自己可以随心所欲使用双指针,看来还是差得远

ListNode *swapPairs(ListNode *head) {
ListNode **p = &head; while (*p && (*p)->next) {
ListNode *t = (*p)->next; (*p)->next = t->next;
t->next = *p;
*p = t; p = &(*p)->next->next;
} return head;
}

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

  1. Swap Nodes in Pairs——LeetCode

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

  2. Swap Nodes in Pairs leetcode java

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  3. Swap Nodes in Pairs LeetCode题解

    做完这个题目,感觉LeetCode的题目出的真好... 这种题,如果让我在面试时候纸上写代码,肯定会挂的. 我昨天晚上看的题目,昨天脑子是懵的,放下了.今天早上来做. 一开始做,提交,果然错了.写的代 ...

  4. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

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

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

  6. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

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

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

  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. redis sets类型及操作

    sets类型及操作set是集合,它是string类型的无序集合.通过hash table实现,添加.删除.查找的复杂度都是0(1).对集合我们可以实现取交际.差集并集.通过这些操作我们可以实现SNS中 ...

  2. 基于ASIO的协程与网络编程

    协程 协程,即协作式程序,其思想是,一系列互相依赖的协程间依次使用CPU,每次只有一个协程工作,而其他协程处于休眠状态.协程可以在运行期间的某个点上暂停执行,并在恢复运行时从暂停的点上继续执行. 协程 ...

  3. easyUI linkbutton组件

    easyUI linkbutton组件: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  4. mac命令行对复杂ipa包重新签名

    最近在做ios的自动化平台,需要通过命令行安装卸载ipa包 好了问题来,别人上传的ipa包,很可能是开发签名了只能在特定手机上安装的测试ipa包,那我们如何将其安装在我们的自动化的iphone上呢? ...

  5. Prim和Kruskal最小生成树

    标题: Prim和Kruskal最小生成树时 限: 2000 ms内存限制: 15000 K总时限: 3000 ms描述: 给出一个矩阵,要求以矩阵方式单步输出生成过程.要求先输出Prim生成过程,再 ...

  6. Windows下Python读取GRIB数据

    之前写了一篇<基于Python的GRIB数据可视化>的文章,好多博友在评论里问我Windows系统下如何读取GRIB数据,在这里我做一下说明. 一.在Windows下Python为什么无法 ...

  7. MEAN教程3-NPM安装

    NPM简介Node只是一个平台,它的功能和API将只是一个最小集.想获得更多的功能,可以使用模块系统来扩展平台.安装.更新和删除Node.js模块最好的方法是使用NPM工具.NPM有如下两个主要特性: ...

  8. 百叶窗特效(用move.js库)

    每逢佳节胖三斤啊,胖了胖了,加上每天坐在电脑前,现在还和一个智障聊天,后天去苏州玩的事情,住哪里啊?去哪里嘿嘿嘿啊? 苏州,找了下,攻略,听说一定要去园林看,听说很牛逼,好吧,陶冶一下我的情操.今天操 ...

  9. 编程练习------C/C++分别实现字符串与整数的转换

    C/C++分别实现字符串与整数的转换 前提:不使用 itoa 和 atoi. 方法一.C和C++通用的一种转换手段是: 1.整数转化为字符串:采用加'0',再逆序的办法,整数加'0'就会隐性转化成ch ...

  10. Wpf中鼠标样式的修改,作用点修改

    最近,在做一个控件的鼠标样式,Ps加了插件,可以编辑生成.cur格式的图标. 可是,所有的改完以后,调试运行,结果发现自己制作的图标的作用点总是在左上角,而不是在"手形"图标的食指 ...