Swap Nodes in Pairs LeetCode题解
做完这个题目,感觉LeetCode的题目出的真好。。。
这种题,如果让我在面试时候纸上写代码,肯定会挂的。
我昨天晚上看的题目,昨天脑子是懵的,放下了。今天早上来做。
一开始做,提交,果然错了。写的代码如下
struct ListNode * swap_2_nodes(struct ListNode *p) //把p指向的两个节点交换位置
{
struct ListNode * q;
if( p == NULL || (q = p->next) == NULL )
{
return p; //不够两个,就放弃转换了。
}
p->next = q->next;
q->next = p; return q;
}
struct ListNode* swapPairs(struct ListNode* head) {
int n = ;
struct ListNode * p,q; p = head;
while(p!= NULL)
{
if( n% == )
{
p = swap_2_nodes(p);
if(n==) head = p;
}
p = p->next;
n ++;
}
return head;
}
这是潜意识里的错误。认为p是指向它的节点,那么p本身就是它前面的节点。这个太容易错了。。。
这个题目的结果就是 我把前两个1/2转换完,3和4确实也转换了,但是第二个节点1指向的仍然是3。也就是把后两个转换了,但是没有通报给前面。
也就是转换完之后,是这样
2->1->3 以及 4->3
很low的错误啊!
一直以为自己链表很熟练了,什么插入删除随便写。
现在看,问题多多啊!不能整天自我感觉良好。。。
归根结底:自己在思考问题的时候,总是偷懒!细节的地方不想去深究!没有搞非常明白,就开始编码。
比如 二分查找的边界。这种问题。
都是写出来,出错了,才根据错误来修改!
加油吧。。改变毛病很难。不改就没法提升。
附上正确答案,分循环和递归两种。
struct ListNode * swap_2_nodes(struct ListNode *p) //把p指向的两个节点交换位置
{
struct ListNode * q;
if( p == NULL || (q = p->next) == NULL )
{
return p; //不够两个,就放弃转换了。
}
p->next = q->next;
q->next = p; return q;
}
struct ListNode* swapPairs(struct ListNode* head) {
int n = ;
struct ListNode * p,q; head = swap_2_nodes(head);
p = head;
//唉!!!一定要记得前面一个节点还有用啊!! 转换3 和 4的时候,前面的2也要链接到啊!
while(p!= NULL)
{
if( n% == )
{
p->next = swap_2_nodes(p->next);
}
p = p->next;
n ++;
}
return head;
}
递归的可读性更强一些,而且思路非常清晰!
struct ListNode* swapPairs(struct ListNode* head) {
struct ListNode * p;
if(head == NULL || (p=head->next) == NULL) return head;
head ->next = swapPairs( p -> next); //先把第三个开始的转换掉,然后用1的next指向他们。
p->next = head; //第2个指向1,然后返回2
return p;
}
Swap Nodes in Pairs LeetCode题解的更多相关文章
- Swap Nodes in Pairs leetcode java
题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 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 ...
- Swap Nodes in Pairs leetcode
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- [LeetCode]Swap Nodes in Pairs题解
Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 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 ...
随机推荐
- Error detected while processing function pythoncomplete#Complete: 错误解决
python vim 环境配置好后,莫名奇妙总是出现:Error detected while processing function pythoncomplete#Complete: 恼人的错误,多 ...
- quicktime player录屏没有声音的解决方法
作者:殊哥链接:https://www.zhihu.com/question/20251726/answer/94938941来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...
- MySQL 8.0的关系数据库新特性详解
前言 MySQL 8.0 当前的最新版本是 8.0.4 rc,估计正式版本出来也快了.本文介绍几个 8.0 在关系数据库方面的主要新特性. 你可能已经知道 MySQL 从版本 5.7 开始提供了 No ...
- psql: could not connect to server: No such file or directory&&PGHOST
由于环境变量 PGHOST配置不当引起的 postgres@pgdb-> psql psql: could not connect to server: No such file or dire ...
- jQuery实现点击控制左右两边元素挤压显示效果
该功能实现的是:分左.右两边布局,左边div默认展开,左边div中有一个元素,点击实现左边div隐藏,右边div挤压过来:再点击实现左边显示,右边挤过去. 一.HTML代码: <div clas ...
- 重识linux-linux主机上的用户信息传递
1 查询用户 w,who,last,lastlog 1)在线用户查询 w ,who 2)账号最近的登录时间 last lastlog 2 用户对谈 write,mesg,wall 1)write ...
- hive sql 语句执行顺序及执行计划
hive 语句执行顺序 from... where.... select...group by... having ... order by... 执行计划 Map Operator Tree: Ta ...
- 1. ibatis 查询的sql列存在相同的列名
如果SQL语句存在两个相同的查询列名,则映射时,取第一个列名的值进行映射 <?xml version="1.0" encoding="UTF-8" ?&g ...
- 在Ubuntu 14.04 64bit上安装百度云Linux客户端BCloud
参考:https://www.cnblogs.com/kluan/p/6014989.html 下载 网盘安装包,Bcloud 是一个 Linux 下超赞的客户端, 官网 github: https: ...
- kubernetes组件架构
kubernetes,简称K8s,是用8代替8个字符“ubernete”而成的缩写.是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效( ...