e.g. 给定链表 1->2->3->4,返回 2->1->4->3 的头节点。

我写了个常见的从头节点遍历,少量的奇数个或偶数个数据都能成功重新排列。但链表过长时结果显示 Time Limit Exceeded 。

 ListNode* swapPairs(ListNode* head) {
ListNode *p = head, *q, *pre;
while (p) {
if (q = p->next) {
if (p == head) head = q;
p->next = q->next;
q->next = p;
if (pre) pre->next = q;
}
pre = p;
p = p->next;
}
return head;
}

看到有一个答案使用二级指针,忘了这种方法了!

pp 从指向 head 指针到指向所有 next 指针遍历下去,p 指向 *pp 表示指向当前结点,q 指向 p 的下一个节点。

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

看到有些答案标题写使用递归,想了下,把两种思路都 DIY 为递归方法,Accepted了。

 ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) return head;
ListNode *p = head, *q = p->next;
if (!q->next)
p->next = NULL;
else
p->next = swapPairs(q->next);
q->next = p;
return q;
}
 ListNode* swapPairs(ListNode* head) {
if (!head || !head->next) return head;
ListNode **pp = &head, *q = (*pp)->next;
(*pp)->next = swapPairs(q->next);
q->next = *pp;
pp = &q;
return *pp;
}

可以看到二级指针代码量确实会少一点,而且使用递归代替迭代有时能减少时间。

但答案这种递归方法最为简单,只用定义一个指针。改为使用二级指针目测无法实现。

 ListNode* swapPairs(ListNode* head) {
if(!head || !head->next) return head;
ListNode *next = head->next;
head->next = swapPairs(next->next);
next->next = head;
return next;
}

【LeetCode】成对交换节点的更多相关文章

  1. LeetCode OJ:Swap Nodes in Pairs(成对交换节点)

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

  2. [LeetCode] 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 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  4. LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

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

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

  6. 我要好offer之 链表大总结

    单链表是一种递归结构,可以将单链表看作特殊的二叉树(我把它叫做一叉树) 单链表的定义: /** * Definition for singly-linked list. * struct ListNo ...

  7. 爬虫_python3_requests

    Requests 网络资源(URLs)撷取套件 改善Urllib2的缺点,让使用者以最简单的方式获取网络资源 可以使用REST操作(POST,PUT,GET,DELETE)存取网络资源 import ...

  8. [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  9. [LeetCode] Integer to Roman 整数转化成罗马数字

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. centos7 修改密码

    Centos7破解密码的方法   Centos7忘记密码   在工作或者自己练习的时候我们难免会大意忘掉自己的root密码,有些同学忘掉密码竟然第一选择是重装系统,工作中可万万使不得! 本篇博客将讲解 ...

  2. tomcat中实现特定路径下的图片的url访问Tomcat配置图片保存路径,图片不保存在项目路径下

    使用Tomcat作为服务器的时候,如果不配置图片保存路径,将图片保存在项目路径下,那么再次打war包发布项目可能会造成图片的丢失,每次重启前将图片先保存再copy到服务器明显不方便,这时可以配置图片保 ...

  3. Python 汉诺塔游戏

    #n 多少个盘子 def hanoi(n,x,y,z): : print(x,'→',z) else: hanoi(n-, x, z,y) #将前n-1个盘子从X移动到y上 print(x,'→',z ...

  4. 【Mysql】key 、primary key 、unique key 与index区别

    参考:https://blog.csdn.net/nanamasuda/article/details/52543177 总的来说,primary key .unique key 这些key建立的同时 ...

  5. codeforces gym 101164 K Cutting 字符串hash

    题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A: 思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N: 题目链接:传送门 #prag ...

  6. sass、less是什么,如何使用?

    一个很好的介绍的SASS,LESS的区别的文档,值get 1.背景介绍 CSS 是一门非程序式语言,没有变量.函数.SCOPE(作用域),需要书写大量看似没有逻辑的代码,不方便维护及扩 展,不利于复用 ...

  7. gc调优我们到底在调整什么

    java开发一般都会涉及到jvm调优其中gc调优是个重点项.那gc调优调整的究竟是什么呢准确来说是业务.下面围绕这个话题展开 起因 为什么说是业务呢得从cc++开始说起如果说是用c/c++做开发运行的 ...

  8. nginx 启动报错 1113: No mapping for the Unicode character exists in the target multi-byte code

    failed (1113: No mapping for the Unicode character exists in the target multi-byte code page) 因为路径有中 ...

  9. ImageConverter引起的 invalid address or address of corrupt block 0xb7feab58 passed to dlfree

    虹软人脸识别,其方法要传NV21格式的byte[], github上有一个虹软的Demo,是不是虹软工作人员写的不清楚,这个Demo里bitmap转NV21格式byte[]用的是一个第三方库https ...

  10. 20165327 2017-2018-2 《Java程序设计》第9周学习总结

    20165327 2017-2018-2 <Java程序设计>第9周学习总结 教材内容总结 第十三章 (一)教材学习内容总结 理解 URL类是对统一资源定位符的抽象,使用URL创建对象的应 ...