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() function
- if(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 ...
随机推荐
- No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 问题
maven编译项目时出错,提示信息如下: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3 ...
- Restful api测试插件Swagger-UI
现在多数的项目开发中,网站和移动端都需要进行数据交互和对接,这少不了使用REST编写API接口这种场景.例如我目前的工作,移动端交由了另一团队开发,不同开发小组之间就需要以规范和文档作为标准和协作基础 ...
- Zookeeper、HBase的伪分布
1.Zookeeper伪分布的部署(3个节点) 所谓的“伪分布式集群”就是在一台服务器中,启动多个Zookeeper实例.“完全分布式集群”是每台服务器,启动一个Zookeeper实例. 1.1.解压 ...
- When you’re nearly 40 and unmarried, and you realize you’re going to be okay
https://medium.com/the-gathering-kind/when-you-re-nearly-40-and-unmarried-and-you-realize-you-re-goi ...
- POJ 2828 Buy Tickets (线段树 单点更新 变形)
题目链接 题意:有N个人排队,给出各个人想插队的位置和标识,要求输出最后的序列. 分析:因为之前的序列会因为插队而变化,如果直接算时间复杂度很高,所以可以用 线段树逆序插入,把序列都插到最后一层,le ...
- 第六届华为创新杯编程大赛-进阶1第1轮 洞穴逃生 (bfs + 优先队列)
这个题容易出错想了挺长时间,然后代码不长,1Y.. 做完题,看了一下别人的博客,也可以优先用 闪烁法术, 在闪烁法术不不如跑步的阶段(即魔法恢复的时候)用跑步. 洞穴逃生 描述: 精灵王子爱好冒险,在 ...
- tomcat发布web service教程
这几天一直在准备找工作,自学了关于web service的一些基本的内容,也遇到了不少问题.现在就把我自己学到的知识和大家分享一下,由于是初学,所以有什么错误的地方请大家帮忙指正,感激不尽~~!! 1 ...
- BZOJ2594: [Wc2006]水管局长数据加强版
题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...
- javascript中的关键字和保留字
javascript中关键字的问题,将名称替换了下,确实就没有问题了.现在将它的关键字和保留字贴出来,便于日后查看和避免在次出现类似的问题. 1 关键字breakcasecatchcontinuede ...
- iphone 如何清空UIWebView的缓存
iphonecachingapplicationcookiescacheperformance I actually think it may retain cached information ...