我的代码是这样的:

class Solution {
public:
ListNode *swapPairs(ListNode *head)
{
const int TRUE = ;
const int FALSE = ;
ListNode *listA;
ListNode *listB;
ListNode *listFront;
ListNode *listTail;
bool bFirstTime = TRUE; listFront = head;
while(listFront != NULL)
{ listA = listFront;
if(bFirstTime)
{
listTail = listFront;
}
listB = listFront->next;
if(!bFirstTime && listB == NULL)
{
return head;
}
if(bFirstTime && listB==NULL)
{
return listA;
}
listFront = listFront->next->next;
if(bFirstTime && listB !=NULL)
{
head = listB;
bFirstTime = FALSE;
}
if(!bFirstTime)
{
listTail->next = listB;
listTail = listA;
}
listB->next = listA;
listA->next = listFront; }
return head;
}
};

网上找的大神的代码:

class Solution {
public:
ListNode *swapPairs(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *cur = NULL, *next = NULL, *tmp = NULL, *pre = NULL;
cur = head;
if(cur != NULL && cur->next != NULL)
head = cur->next;
while(cur != NULL)
{
if(cur->next == NULL)
{
return head;
}
next = cur->next;
if(pre != NULL)
pre->next = next;
tmp = next->next;
next->next = cur;
cur->next = tmp;
pre = cur;
cur = cur->next; }
return head; }
};

这个问题主要考虑的是两个节点交换位置时,后续节点指针信息的保存。A->B->C->D,经过一次变换后B->A->C->D->E,此时不能丢失指向A的指针pTail,因为一次变换后标记指针已经移动到下一次的处理单位,即pA指向C,pB指向D,pFront指向E,第二次交换若没有pTail的变化会成为B->A->C<-D,链表丢失了D元素且交换失败。因此在第二次交换多出的步骤是将只想A的指针pTail->next = pB;pTial = pA;完成正确的首位相连。以上是一个主要的交换思路。
    此外考虑的是程序的结束标志,考虑的是只有一个输入时,和若干个输入时的ptr->next的值是否是NULL,我的程序加入了一个bFirst使得考虑情况很复杂化,观察他人的代码,直接用了if(curr!=NULL && cur->next!=NULL) 来对确定为头节点,这里当只有一个节点时,它不执行。而在循环里边使用if(curr->next == NULL) 来作为结束标志return head; 程序的结构明了。其中pTail的交换如前所述。

总结:程序的结束条件,初始条件必须明了简单。

LeetCode 2 :Swap Nodes in Pairs的更多相关文章

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

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

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

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

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

  4. [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)

    Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...

  5. LeetCode 024 Swap Nodes in Pairs

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

  6. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  7. 【leetcode】Swap Nodes in Pairs (middle)

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

  8. Java for LeetCode 024 Swap Nodes in Pairs

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

  9. leetcode:Swap Nodes in Pairs

    Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...

  10. Java [leetcode 24]Swap Nodes in Pairs

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

随机推荐

  1. Python 3基础教程27-字典

    这篇来介绍Python中的字典.字典一般用大括号包裹起来,里面的元素都是有键和值组成. # 字典 # 我们随便设计几个城市的明天的最高温度tem ={'北京':22,'上海':23,'深圳':24,' ...

  2. bug单的提交

    顶头信息 所属产品,所属项目,所属模块,影响版本,当前指派,bug类型:代码错误,界面优化,设计缺陷,性能问题,标准规范,其他,安全相关.bug标题,严重程度,优先级 缺陷描述 bug描述,预置条件, ...

  3. drf 缓存扩展

    drf缓存给了一个非常方便的扩展,使用起来相当方便 1-   安装 pip install drf-extensions 2-配置 在settings里面增加两项配置 # drf扩展REST_FRAM ...

  4. (原创)不过如此的 DFS 深度优先遍历

    DFS 深度优先遍历 DFS算法用于遍历图结构,旨在遍历每一个结点,顾名思义,这种方法把遍历的重点放在深度上,什么意思呢?就是在访问过的结点做标记的前提下,一条路走到天黑,我们都知道当每一个结点都有很 ...

  5. Toward Convolutional Blind Denoising of Real Photographs

    本文提出了一个针对真实图像的盲卷积去噪网络,增强了深度去噪模型的鲁棒性和实用性. 摘要 作者提出了一个 CBD-Net,由噪声估计子网络和去噪子网络两部分组成. 作者设计了一个更加真实的噪声模型,同时 ...

  6. 软件工程项目组Z.XML会议记录 2013/11/06

    软件工程项目组Z.XML会议记录 [例会时间]2013年11月06日星期二21:00-22:00 [例会形式]小组讨论 [例会地点]三号公寓楼会客厅 [例会主持]李孟 [会议记录]薛亚杰 会议整体流程 ...

  7. CFS 调度器

    CFS调度器的原理明白了但是有个地方,搜遍了整个网络也没找到一个合理的解释: if (delta > ideal_runtime) resched_task(rq_of(cfs_rq)-> ...

  8. 在.net2.0下使用System.Web.Script.Serialization;

    最近,在弄json字符串转为对象.需要添加这个引用System.Web.Script.Serialization;因为版本必须是dotnet2.0的原因,发现很多解决方案不适合自己.故使用这种解决办法 ...

  9. P1140 相似基因

    题目背景 大家都知道,基因可以看作一个碱基对序列.它包含了4种核苷酸,简记作A,C,G,T.生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物. 在一个人类基因工作组的任务中,生物学家研究 ...

  10. java高精度类尝试

    java高精度尝试, poj2109,比较坑的题目 import java.io.*; import java.util.*; import java.math.*; public class Mai ...