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 ...
随机推荐
- 廖雪峰Java5Java集合-5Queue-1使用Queue
Queue特性和基本方法 Queue实现一个先进先出(FIFO, First In First Out)的队列.如收银台排队支付. Java中LinkedList实现了Queue接口,可以直接把Lin ...
- 廖雪峰Java2面向对象编程-2数据封装-1方法重载
方法重载 方法重载Overload是指:多个方法的方法名相同,但各自的参数不同 参数的个数不同 参数的类型不同 参数位置不同 方法返回值类型通常都是相同的 目的:相同功能的方法使用同一名字,便于调用 ...
- 使用Spring MockMVC对controller做单元测试(转)
https://www.cnblogs.com/ylty/p/6420738.html 1.对单一controller做测试. import org.junit.Before; import org. ...
- tesseract 训练
下载chi_sim.traindata字库下载tesseract-ocr-setup-3.02.02.exe 下载地址:http://code.google.com/p/tesseract-ocr/d ...
- Centos7下的systemctl命令与service和chkconfig
博主使用的操作系统是最新的CentOS 7,所以可能和网上一些老的博文有一定出入,那是因为版本更新的原因. 这里写图片描述1 service service命令用于对系统服务进行管理,比如启动(sta ...
- centos6和centos7的防火墙的操作
1:centos6的两种方式 1.1:service方式 查看防火墙状态: [root@centos6 ~]# service iptables status iptables:未运行防火墙. 开启防 ...
- Mybatis 系列7-结合源码解析核心CRUD 配置及用法
[Mybatis 系列10-结合源码解析mybatis 执行流程] [Mybatis 系列9-强大的动态sql 语句] [Mybatis 系列8-结合源码解析select.resultMap的用法] ...
- U3D学习002——编辑器使用
shift键+鼠标点击中间方块,实现视角切换回初始化透视模式 Unity GameObject菜单栏下有3个和View(此View指显示Scene面板虚拟相机(后面简称Scene Camera)的视角 ...
- cocos源码分析--SpriteBatchNode绘图原理
SpriteBatchNode继承Node,并实现了TextureProtocol接口,重写了Node的addChild()方法,visit()方法以及draw()方法. addChild()方法限制 ...
- cocos设置 相机矩阵和投影矩阵 源码浅析
在cocos中,最后设置视口大小,相机矩阵,裁剪矩阵是在setProjection方法中,源码如下: void Director::setProjection(Projection projectio ...