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 ...
随机推荐
- 学习笔记之Nginx
NGINX | High Performance Load Balancer, Web Server, & Reverse Proxy https://www.nginx.com/ flawl ...
- 1121 Damn Single (25 分)
1121 Damn Single (25 分) "Damn Single (单身狗)" is the Chinese nickname for someone who is bei ...
- Go语言 函数,工程管理
Go语言 函数,工程管理 1.无参无返回值函数的使用 package main import "fmt" func main() { // 无参无返回值函数的调用:函数名() fu ...
- 2-java内省机制(Introspector)
来一个简单的示例吧 package com.my.test; import java.beans.BeanInfo; import java.beans.Introspector; import ja ...
- struts2的result的类型配置简介
一.在strut2的action处理完成后,就应该向用户返回结果信息result 根据以下代码作为实例分析: <package name="Hello" extends=&q ...
- DNS(bind)服务器安装和配置
一.前言 DNS 域名系统(英文:Domain Name System,缩写:DNS)是因特网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.DNS使用TCP ...
- phalcon7开发环境搭建
1.安装apahce服务器 2.安装mysql 3.安装php7 4.编译和安装phalcon7 git clone --depth=1 git://github.com/dreamsxin/cpha ...
- asp.net 基础内容
1. ViewData ViewBag TempData 区别? 1.ViewData和TempData是字典类型,赋值方式用字典方式,ViewData["myName"] 2. ...
- [java,2018-02-01] quartz定时任务中时间表达式
格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 ...
- 微信小程序笔记<五> 页面管理及生命周期(route)——getCurrentPages()
在小程序中所有页面的路由全部由框架进行管理,而框架以栈的形式维护了当前的所有页面. 当发生路由切换时,页面栈的表现: getCurrentPages() 用于获取当前页面栈的实例,可以把 getCur ...