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. Find the Duplicate Number 解答

    Question Given an array nums containing n + 1 integers where each integer is between 1 and n (inclus ...

  2. 【HDU1166】敌兵布阵(树状数组或线段树)

    是一道树状数组的裸题,也可以说是线段树的对于单点维护的裸题.多做这种题目可以提高自己对基础知识的理解程度,很经典. #include <iostream> #include <cst ...

  3. 计算机程序的思维逻辑 (63) - 实用序列化: JSON/XML/MessagePack

    上节,我们介绍了Java中的标准序列化机制,我们提到,它有一些重要的限制,最重要的是不能跨语言,实践中经常使用一些替代方案,比如XML/JSON/MessagePack. Java SDK中对这些格式 ...

  4. Runtime运行时机制

    Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的 我们需要了解的是 Objective-C 是一门动态语言, ...

  5. Maven中pom.xml常用元素说明

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  6. [Javascript] How to write a Javascript libarary

    Create package.json file //npm settings npm set init-author-name 'username' npm set init-author-emai ...

  7. 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6621566 上一篇文章Android进程间通信 ...

  8. 对Lucene PhraseQuery的slop的理解[转载]

    所谓PhraseQuery,就是通过短语来检索,比如我想查“big car”这个短语,那么如果待匹配的document的指定项里包含了"big car"这个短语,这个documen ...

  9. 仿QQ好友列表界面的实现

    TableView有2种style:UITableViewStylePlain 和 UITableViewStyleGrouped. 但是QQ好友列表的tableView给人的感觉似乎是2个style ...

  10. QT5.5实现串口通信

    QT5.1以上版本自带QtSerialPort集成库,只要在头文件中集成 #include <QtSerialPort/QSerialPort> #include <QtSerial ...