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 ...
随机推荐
- android-sdk和api版本
Platform Version API Level VERSION_CODE Notes Android 8.1 27 O_MR1 平台亮点Android 8.0 26 O 平台亮点An ...
- XXE注入攻击与防御
在研究XXE注入攻击之前先了解一下什么是XXE 定义 XML用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.XML文档结构包括 ...
- C语言:传值,传地址
形参:形式参数实参:实际参数 传值: 把实参的值复制给形参, 修改函数内的形参,不会影响实参. 传地址: 指针传值,形参为指向实参地址的指针 当对形参的指向操作时,相当于对实参本身进行的操作 #inc ...
- 用VirtualBox快速安装虚拟机virtual Machine(Win7+IE10)
前端测试,经常需要各种环境, 用“Virtual Box + OVA文件”安装虚拟机, 是简单高效的一种方法,可以安装各种window和IE的版本.下面以IE10 + Win7为例说明. 1) 下载和 ...
- http请求状态及其含义表
1xx - 信息提示 100 - 初始的请求已经接受,客户应当继续发送请求的其余部分.(HTTP 1.1新) 101 - 服务器将遵从客户的请求转换到另外一种协议.(HTTP 1.1新) 2xx ...
- win10 java1.7安装笔记
博主不选择安装C盘,选择在D盘安装,新建Java文件夹,在Java文件夹中新建两个子文件夹,一个命名jdk1.7,一个命名jre,如果不区分目录安装jdk和jre,会导致一些文件的缺失,导致一些错误, ...
- 解决Centos7不能联网且ifconfig出现command not found
在虚拟机中以最小化方式安装centos7,后无法上网,因为centos7默认网卡未激活. 而且在sbin目录中没有ifconfig文件,这是因为centos7已经不使用 ifconfig命令了,已经用 ...
- Missing artifact javax.transaction:jta:jar:1.0.1B
下载https://pan.baidu.com/s/1hsfyj8S到某目录,比如: /Users/yintingting/Downloads 打开terminal,cd /Users/yinting ...
- WPF DataGrid 数据绑定、样式、分页、增删改查,连接Access数据库
先上效果图: XAML: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...
- SVG 学习<一>基础图形及线段
目录 SVG 学习<一>基础图形及线段 SVG 学习<二>进阶 SVG世界,视野,视窗 stroke属性 svg分组 SVG 学习<三>渐变 SVG 学习<四 ...