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. KDTree详解及java实现

    本文内容基于An introductory tutoril on kd-trees 1.KDTree介绍 KDTree根据m维空间中的数据集D构建的二叉树,能加快常用于最近邻查找(在加快k-means ...

  2. 利用智能手机(Android)追踪一块磁铁(三)

    更新磁铁追踪算法的源代码,Android Studio项目工程 github地址:https://github.com/amazingyyc/MagnetLocate 说明:将磁铁的位置信息封装成消息 ...

  3. JavaScript - 基于原型的面向对象

    JavaScript - 基于原型的面向对象 1. 引言 JavaScript 是一种基于原型的面向对象语言,而不是基于类的!!! 基于类的面向对象语言,比如 Java,是构建在两个不同实体的概念之上 ...

  4. AC Milan VS Juventus(模拟)

    AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Oth ...

  5. 导致flash屏幕重绘的几种方式及避免重绘的方法

    导致屏幕重绘的几种原因: 1.最常见的是情况就是舞台上的可视组件在形状.位置.状态(alpha, scale…)发生改变的时候会触发Flash Player 的重绘. 2.当一个DisplayObje ...

  6. progressbar使用方法:进度画面大小,进度画面背景,进度百分比

    前一段时间,因为项目须要研究了下progressbar,发现这个小东西还真是不简单.在这个小控件的显示效果上,花费的时间远大于预估的工作量.话说程序猿一直是这样,预估的工作量远少于实际...      ...

  7. linux修改系统时间

    当你把linux还原到某个点的时候,vmware帮不了你把系统时间也给重设了.所以这时候就要手工来搞.关于咋设linux时间.网上介绍也很多,但是都是抄来抄去的东西.那怎么才能高效快捷的设置系统时间呢 ...

  8. 使用 ETag 和 Last-Modified 报头减轻服务器压力(转)

    介绍你的网站在并发访问很大并且无法承受压力的情况下,你会选择如何优化?很多人首先会想从服务器缓存方面着手 对程序进行优化,许多不同的服务器缓存方式都有他们自己的特点,像我曾经参与的一些项目中,根据缓存 ...

  9. 如何在WP8模拟器中连接本地的web服务

    这个问题困扰了很久,查找答案一度找偏方向. 其实连接web服务对于wp7不是问题,因为wp7使用的网络就是本机的网络,但是到了wp8模拟器,纯粹的虚拟机,独立的设备,也就有了自己的网络连接,要当做虚拟 ...

  10. SpringMvc学习-环境搭建

    最近在学习SpringMVC,首先来说说SpringMVC项目的搭建. 1.SpringMVC简介 spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级W ...