做完这个题目,感觉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题解的更多相关文章

  1. Swap Nodes in Pairs leetcode java

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-> ...

  2. Swap Nodes in Pairs——LeetCode

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

  3. Swap Nodes in Pairs leetcode

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

  4. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  5. [LeetCode]Swap Nodes in Pairs题解

    Swap Nodes in Pairs: Given a linked list, swap every two adjacent nodes and return its head. For exa ...

  6. [Leetcode][Python]24: Swap Nodes in Pairs

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...

  7. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  8. 【LeetCode练习题】Swap Nodes in Pairs

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  9. 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 ...

随机推荐

  1. 浙江财经大学第十五届大学生程序设计竞赛------B 烦恼先生打麻将

    问题 B: B - 烦恼先生打麻将 时间限制: 1 Sec  内存限制: 256 MB提交: 8  解决: 5[提交][状态][讨论版] 题目描述 输入 6 6 Z D 1S 1S 9W 5W 2S ...

  2. java多线程——监视锁(monitor)(转)

    https://blog.csdn.net/hqq2023623/article/details/51000153 java中每个对象都有唯一的一个monitor,想拥有一个对象的monitor的话有 ...

  3. 部署redis5.0.3

    一.准备环境 1.下载 # wget http://download.redis.io/releases/redis-5.0.3.tar.gz [root@localhost ~]# wget htt ...

  4. Jmeter(二十七)Jmeter Question 之“集成Ant+Jenkins自动化”

    首先介绍一下Ant.Apache Ant,是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.由Apache软件基金会所提供. 是的.还是Apache家 ...

  5. [UE4]如何编译部署独立专用服务端(Standalone Dedicated Server)

    这是论坛上对UE服务端功能的回答,意思是UE4提供了网游服务端所具备的特性,包括位移修正.物理碰撞检测.这些特性不是UE4才加入,早期UE版本就有了. https://answers.unrealen ...

  6. [UE4]客户端-服务器模式

    客户端负责表现.服务器端负责数据. 以掉血为例: 一.玩家A砍了B一刀 二.服务器计算伤害,修改B的血量 三.把B的血量发给B,A砍B的动作发给所有能看到的玩家 四.客户端播放掉血量(如果允许可见), ...

  7. IE浏览器强制不是要兼容视图

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...

  8. typescript函数类型接口

    /* 接口的作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用.接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据, ...

  9. 在UnrealEngine4中使用Google Protobuf

    转自:https://blog.csdn.net/or_7r_ccl/article/details/54986393 在UnrealEngine4中使用Google Protobuf         ...

  10. java使用jedis访问CentOS中的redis

    pom.xml <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</ ...