Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

相邻两个节点为一组,交换一组内节点的位置,如1和2交换,3和4交换。

不能改变节点里values的值,而且只能使用常量的空间。

解题思路:

  • 设置一个dummy头节点方便对第一个节点进行操作。
  • 然后有三个指针 pre,first,second。 first和second分别指向待交换的第一个和第二个节点,pre指针指向first之前的那个节点。
  • 交换first和second指针的节点,交换过程如图:

  • 交换后,判断first节点后面有没有节点了,有一个还是有两个节点。
  • 如果后面有一个节点或者没有节点,链表交换完毕,退出循环。
  • 如果后面有两个节点,first后移,设置pre和second的新位置,再次执行上面的交换操作。
  • 循环退出后,链表已经交换了顺序了。
  • 删除dummy节点,返回head指针。

代码如下:

class Solution {
public:
ListNode *swapPairs(ListNode *head) {
if(head == NULL)
return NULL;
ListNode *dummy = new ListNode();
dummy->next = head;
ListNode *pre = dummy,*first = head,*second = head->next; if(second == NULL){
//只有一个节点
delete dummy;
return head;
}
else{
//先交换第一个和第二个节点
pre->next = second;
first->next = second->next;
second->next = first;
} while(true){
if(first->next == NULL || first->next->next == NULL)
//first后面没有节点或者只有一个节点
break;
else{
//有两个节点
pre = first;
first = first->next;
second = first->next; pre->next = second;
first->next = second->next;
second->next = first;
}
}
head = dummy->next;
delete dummy;
return head;
}
};

【LeetCode练习题】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. linux中的文件结构

    linux下的文件结构,看看每个文件夹都是干吗用的 /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的 ...

  2. Poj3074-Sudoku(数独DLX)

    题意: 给出一个9*9的矩阵,有一些格子已经填了数,有一些是.代表未填.求任意一组解使得每行包含1~9,每列包含1~9,每个小矩形(3*3)包含1~9. 解析: 精确覆盖DLX的经典题目,每一行代表要 ...

  3. JSTL解析——007——fmt标签库02

    各位亲们,近期事情比较多,没更新,come on! 1.<fmt:bundle>/<fmt:message>/<fmt:param>资源国际化标签 java中使用R ...

  4. AsyncTask实现登录功能,上传图片,get,post

    提交成功时,从服务器端返回数据“load success” 用户名.密码正确后成功登录,并且在服务器端的文件保存目录上看到了从客户端上传的图片. 客户端代码: MainActivity.java im ...

  5. Direct3D 纹理映射

    纹理映射是将2D的图片映射到一个3D物体上面,物体上漂亮图案被称为纹理贴图, 一个表面可以支持多张贴图等等,下面简单介绍下纹理贴图 纹理贴图UV: 贴图是一个个像素点组成,每一个像素点都由一个坐标最后 ...

  6. LeetCode227:Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  7. My way to Python - Day02

    版权声明: 本文中的资料均来自于互联网.将各路内容摘抄于此,作为学习笔记,方便用作后面翻阅查看.如果原作者对文中内容的引用有任何版权方面的问题,请随时联系,我将尽快处理. 特别鸣谢:武沛齐 <P ...

  8. attr()和prop()的区别

    引用以为一位大神的文章: http://www.365mini.com/page/jquery-prop.htm http://www.365mini.com/page/jquery-attr-vs- ...

  9. Intellij Idea安装主题

    IDEA中jar包形式的主题比较常见.(顺便给大家推荐一个主题站:http://www.ideacolorthemes.org/themes/) 从主菜单中依次选择[File]>[Import ...

  10. Eclipse闪退解决办法

    解决方式: 1.通过在命令行中输入“where java”,找到除jdk目录下的所有java相关程序,直接删掉(一般会在C:\WINDOWS\system32下) 2.内存不足,打开Eclipse目录 ...