leetcode—Swap Nodes in Pairs
1.题目描述
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.
2.解法分析
/*** 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) {// Start typing your C/C++ solution below// DO NOT write int main() functionif(head==NULL)return NULL;ListNode *cur=head;//增加一个头结点有利于是代码整齐ListNode* myHead=new ListNode(-1);ListNode* prev=myHead;prev->next=cur;while(cur!=NULL&&cur->next!=NULL){prev->next=cur->next;cur->next=cur->next->next;prev->next->next=cur;prev=cur;cur=cur->next;}prev=myHead->next;delete myHead;return prev;}};
leetcode—Swap Nodes in Pairs的更多相关文章
- 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]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 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...
- Leetcode: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->2-& ...
- [Leetcode] Swap nodes in pairs 成对交换结点
Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...
- LeetCode Swap Nodes in Pairs 交换结点对(单链表)
题意:给一个单链表,将其每两个结点交换,只改尾指针,不改元素值. 思路:迭代法和递归法都容易写,就写个递归的了. 4ms /** * Definition for singly-linked list ...
- leetcode Swap Nodes in Pairs python
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
随机推荐
- 63. Unique Paths II
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- openfire的smack和asmack
smack你可以看成是一套封装好了的用于实现XMPP协议传输的API,它是一个非常简单并且功能强大的类库,给用户发送消息只需要三行代码.下载地址:http://www.igniterealtime.o ...
- 10位顶级PHP大师的开发原则
在Web开发世界里,PHP是最流行的语言之一,从PHP里,你能够很容易的找到你所需的脚本,遗憾的是,很少人会去用“最佳做法”去写一个PHP程序.这里,我们向大家介绍PHP的10种最佳实践,当然,每一种 ...
- showdialog()与show的区别
showdialog就是显示有模式的窗体,showdialog后面的语句不会执行,直到显示的窗体被关闭. show就是无模式的窗体,显示窗体后不论窗体是否关闭都执行show后面的语句. ------- ...
- c# webbrowser 随机点击链接
HtmlElementCollection hec = webBrowser1.Document.All; ; i < hec.Count; i++) { if (hec[i].GetAttri ...
- BestCoder Round #2 1001 (简单处理)
题目链接 题意:给N条信息,每个信息代表有x个人从开始的时间 到 结束的时间在餐厅就餐, 问最少需要多少座位才能满足需要. 分析:由于时间只有24*60 所以把每个时间点放到 数组a中,并标记开始的时 ...
- CodeForces 489C (贪心) Given Length and Sum of Digits...
题意: 找出m位且各个数位数字之和为s的最大和最小整数,不包括前导0(比如说003是非法的),但0是可以的. 分析: 这题是用贪心来做的,同样是m位数,前面的数字越大这个数就越大. 所以写一个can( ...
- ADO和DAO的区别
ADO(ACTIVEX DATA OBJECTS)应用层的数据访问接口ODBC 数据库驱动接口OLE DB 系统级数据访问接口DAO (DATA ACCESS OBJECTS) 对象的数据访问接口AD ...
- MDEV Primer
/************************************************************************** * MDEV Primer * 说明: * 本文 ...
- Remove Duplicates from Sorted List I & II
Title: Given a sorted linked list, delete all duplicates such that each element appear only once. Fo ...