LeetCode24 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.(Easy)
分析:
链表题,画个图就比较清晰了,就是每两个进行一下翻转,注意翻转部分头尾指针的指向即可,头指针会变,所以用下dummy node。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode dummy();
dummy.next = head;
head = &dummy;
while (head -> next != nullptr && head -> next -> next != nullptr) {
ListNode* temp1 = head -> next;
head -> next = head -> next -> next;
ListNode* temp2 = head -> next -> next;
head -> next -> next = temp1;
temp1 -> next = temp2;
head = head -> next -> next;
}
return dummy.next;
}
};
LeetCode24 Swap Nodes in Pairs的更多相关文章
- Leetcode-24 Swap Nodes in Pairs
#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- Leetcode24.Swap Nodes in Pairs两两交换链表中的节点
给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. 说明: 你的算法只能使用常数的 ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- [LintCode] Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. Example Given 1->2-> ...
- 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- 【LeetCode练习题】Swap Nodes in Pairs
Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...
- [Leetcode][Python]24: Swap Nodes in Pairs
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 24: Swap Nodes in Pairshttps://oj.leetc ...
- 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 线性表 Swap Nodes in Pairs
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Swap Nodes in Pairs Total Accepted: 12511 Tota ...
随机推荐
- Having the Result Set of a Stored Proc Sent to You by RSS Feed.
Having the Result Set of a Stored Proc Sent to You by RSS Feed. by JBrooks 14. 十二月 2010 12:44 I wa ...
- 【转】从零开始编写自己的C#框架(7)——需求分析
转自:http://www.cnblogs.com/EmptyFS/p/3653934.html 本章内容虽然叫“需求分析”,实际上关于具体的需求分析操作步骤并没有深入去写,因为细化的话那将是一本厚厚 ...
- dom 左右两侧得广告(兼容IE FF)
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 好用的shell命令行: fish的配置
fish的可视化配置命令: $ fish_config 其配置文件夹为 ~/.config/fish. 1.要设置环境变量,在配置文件夹里新建 config.fish 文件,它会作为fish 启动时的 ...
- js获取时间搓
var oData=new Date().getTime(2016-01-16); console.log(oData);
- Gridheh 垂直居中
Gridheh 垂直居中 上下居中 each columns set layout ColumnDefValues.Layout = tlCenter 有colResize,拖动调整列宽. 但是没有 ...
- Hadoop Java开发实用快捷键收藏
不断总结更新.... Alt + / 补全 Ctrl + T 打出结构 Ctrl + 2 ,再选择 Quick Assist - Assign to local variable Ctrl ...
- http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-open-and-save-it-using-dsoframer.aspx
http://blogs.msdn.com/b/pranavwagh/archive/2007/03/03/word-2007-file-seems-to-be-deleted-when-you-op ...
- Ubuntu 14.04 LTS中怎样解决系统设置残缺的问题
iBus卸载之后,系统设置会缺失,是Ubuntu 14.04和iBus之间的关系引起的,但是如果直接安装 sudo apt-get install ubuntu-desktop 这个会把amazon广 ...
- 建表的sql
1. 创建用户表 create table user( id int unsigned not null primary key auto_increment comment '自增id', user ...