LeetCode OJ--Swap Nodes in Pairs
https://oj.leetcode.com/problems/swap-nodes-in-pairs/
链表的处理
/**
* 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 = new ListNode(-);
if(head == NULL)
return dummy->next; dummy->next = head; ListNode *tail = dummy; ListNode *current = head;
ListNode *second = head->next;
while(current && second)
{
tail->next = second;
current->next = second->next;
second->next = current; current = current->next;
if(current == NULL)
break;
second = current->next;
tail = tail->next;
tail = tail->next; }
return dummy->next;
}
};
LeetCode OJ--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 【 Swap Nodes in Pairs 】python 实现
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- [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-> ...
随机推荐
- 2015年第六届蓝桥杯C/C++程序设计本科B组决赛
1.积分之谜(枚举) 2.完美正方形 3.关联账户(并查集) 4.密文搜索 5.居民集会 6.模型染色 1.积分之迷 小明开了个网上商店,卖风铃.共有3个品牌:A,B,C.为了促销,每件商品都会返固定 ...
- ios 屏幕概况
转:http://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions
- math汇总
**** 1/(1^2) + 1/(2^2) + … + 1/(n^2)会收敛到pi^2/6,当n的数位大于6位数字时,最后的结果就是pi^2/6 ****** &的大作用 1.先看看这个n= ...
- EntityFramework+WCF
首先需要在服务对象实例上面添加数据契约[DataContract]和 [DataMember],当然直接在类中修改也可以,但是对于tt模板来说一旦保存以后数据就会重新生成, 所以得在tt模板中修改 ...
- 初学javaweb,远离各自框架
OSCHINA 软件库有一个分类--Web框架,该分类中包含多种编程语言的将近500个项目. Web框架是开发者在使用某种语言编写Web应用服务端时关于架构的最佳实践.很多Web框架是从实际的Web项 ...
- 將後台的Json數據返回到前台
前台JS代碼 $.post('/Book/GetBookClassIDByName', { BookName: "旅遊手冊" }, function (data) { if (da ...
- VC++ 将资源位图画到窗口上去的方法
第一种方法最简单:直接看代码 CImage img; img.LoadFromResource(AfxGetInstanceHandle(), IDB_BITMAP1); img.Draw(*pDc, ...
- Ajax 结构及使用
AJAX AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaSc ...
- js 获取 通过 ”?“ 或者 ”&“ url 传过来参数值
请先 引用 jQuery的js <script> String.prototype.GetValue=function(para) { var reg = new RegExp(" ...
- Metadata file 'xxx.dll' could not be found 已解决
最近学习三层架构,在网上找了个权限管理的源码研究,发现编译不通过,到处都是Metadata file 'xxx.dll' could not be found,找了两天原因都没找到答案. 然后试着去编 ...