我的代码是这样的:

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. mybatis <collection>标签 类型为string时无法获取重复数据错误

    1.场景: fyq_share_house 表 和 fyq_sh_tag 表 两张表是一对多的关系, 一个楼盘对应多个标签,在实体类ShareHouse中使用 /** * 楼盘标签 */ privat ...

  2. 重写selenium 的 click()操作,使其变成隐式等待

    selenium 页面常会因为页面加载慢而出现element 不能被点击到的情况,比如加载过程中出现遮罩,导致element 可见不可点.以下方法重写click(),用隐式等待解决这个问题. 基本思路 ...

  3. CodeForces - 948C(前缀和 + 二分)

    链接:CodeForces - 948C 题意:N天,每天生产一堆雪体积 V[i] ,每天每堆雪融化 T[i],问每天融化了多少雪. 题解:对 T 求前缀和,求每一堆雪能熬过多少天,再记录一下多余的就 ...

  4. 1018 Public Bike Management (30 分)(图的遍历and最短路径)

    这题不能直接在Dijkstra中写这个第一 标尺和第二标尺的要求 因为这是需要完整路径以后才能计算的  所以写完后可以在遍历 #include<bits/stdc++.h> using n ...

  5. HDU 4588 Count The Carries(数学统计)

    Description One day, Implus gets interested in binary addition and binary carry. He will transfer al ...

  6. nopcommerce商城系统--文档整理

    原址:http://www.nopcommerce.com/documentation.aspx nopCommerce文档可以帮助您一步一步的搭建属于您自己的在线商城.根据该文档说明,您可以选择您想 ...

  7. lintcode-93-平衡二叉树

    93-平衡二叉树 给定一个二叉树,确定它是高度平衡的.对于这个问题,一棵高度平衡的二叉树的定义是:一棵二叉树中每个节点的两个子树的深度相差不会超过1. 您在真实的面试中是否遇到过这个题? Yes 样例 ...

  8. WEBSTORM中html文件运行之后出现乱码的问题解决

    出现如下问题: 解决方案: 1.点击"文件编码" 2.选择GBK 3.点击Reload. 4.此时,源代码中的中文字体会变成乱码,把这些乱码重新输入成原先的中文.然后运行html文 ...

  9. 【bzoj3052】[wc2013]糖果公园 带修改树上莫队

    题目描述 给出一棵n个点的树,每个点有一个点权,点权范围为1~m.支持两种操作:(1)修改一个点的点权 (2)对于一条路径,求$\sum\limits_{i=1}^m\sum\limits_{j=1} ...

  10. Java Integer比较

    今天看微信做了一个选择题,对Integer比较结果有点意外,题目如下: public static void main(String[] args) { Integer a = 1; Integer ...