【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given1->2->3->4, you should return the list as2->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的更多相关文章
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- 【LeetCode】Swap Nodes in Pairs 解题报告
Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...
- 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 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆☆☆(链表,相邻两节点交换)
Swap Nodes in Pairs 描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4 ...
- LeetCode 024 Swap Nodes in Pairs
题目描述:Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- [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 ...
- 【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 ...
- 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-& ...
- leetcode:Swap Nodes in Pairs
Given a linked list, swap every two adjacent(相邻的) nodes and return its head. For example,Given 1-> ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...
随机推荐
- Linux2.6内核 -- 编码风格(3)
9.typedef 内核开发者们强烈反对使用 typedef 语句.他们的理由是: 1> typedef 掩盖了数据的真实类型 2> 由于数据类型隐藏起 ...
- goahead webserver源码分析
1.一个txt文本架构图 main() | |--websOpenServer() | |-- websOpenListen() | ...
- 盒子模型&position定位
有时候深深的感觉语文这门课程其实很有用, 至少以前学的时候没有感觉到 直到现在阅读大量的别人的资料文章的时候或者是看一些题目....... 总之:认真阅读小心品味 当然,前面的孤言自语和本文无关,只是 ...
- 跨服务器查询sql语句样例
若2个数据库在同一台机器上:insert into DataBase_A..Table1(col1,col2,col3----)select col11,col22,col33-- from Data ...
- Memcached完全解剖–1. memcached基金会
翻译一篇技术评论社的文章,是讲memcached的连载.fcicq同学说这个东西非常实用,希望大家喜欢. 发表日:2008/7/2 作者:长野雅广(Masahiro Nagano) 原文链接:ht ...
- 内外连接、组函数、DDL、DML和TCL
前言 cross join ,是笛卡尔积:nature join 是自然连接. 正文 内外连接 inner join inner join 的inner能够省略. 内连接 在一个表中可以找到在还有一个 ...
- 向html某个元素中添加信息
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- OpenCV——CvSeq动态结构序列
动态结构序列CvSeq是所有OpenCV动态数据结构的基础. 分为两类: 稠密序列 稀疏序列 (1) 稠密序列都派生自CvSeq,他们用来代表可扩展的一维数组 - 向量.栈.队列和双端队列.数据间不存 ...
- vim 折叠技巧
转自:http://www.2cto.com/os/201203/122133.html 主要命令: zf-创建折叠 zf20G--创建折叠,从当前行折叠到第20行 zfgg--创建折叠,从当前位置折 ...
- [转载]OpenSUSE 13.2/13.1 安装搜狗拼音输入法
1. 添加 M17N 源: 13.2: sudo zypper ar -f http://download.opensuse.org/repositories/M17N/openSUSE_13.2/ ...