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 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
/**
* 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* p=head;
ListNode* q=p;
while(p!=NULL&&p->next!=NULL)
{
ListNode* newlist=new ListNode(p->val);
p->val=p->next->val;
newlist->next=p->next->next;
p->next=newlist;
p=p->next->next;
}
return q;
}
};
Leetcode-24 Swap Nodes in Pairs的更多相关文章
- 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] 24. Swap Nodes in Pairs 成对交换节点
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- Java [leetcode 24]Swap Nodes in Pairs
题目描述: 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. For example, Given 1->2-& ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...
- (链表 递归) 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]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 24 Swap Nodes in Pairs (交换相邻节点)
题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description Problem: 交换相邻的两个节点 如上 ...
- [LeetCode] 24. Swap Nodes in Pairs ☆
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
随机推荐
- ipad上自定义view的旋转适配
ios8横屏时宽高会自动转换,但是ios7不是 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; CGFloat scree ...
- System.getProperty()方法大全
System.out.println("当前程序所在目录:" + System.getProperty("user.dir")); // 当前程序所在目录 Sy ...
- twitter.common.concurrent deadline and defer
此defer非golang中的defer https://tour.golang.org/flowcontrol/12 from twitter.common.concurrent import Ti ...
- csv
csv 文件的读写:http://www.cnblogs.com/fiozhao/p/3225112.html 求取一个立方体的对角线穿个的边长为1的正方体的个数:http://www.cnblogs ...
- REGEXP 正则的实现两个字符串组的匹配。(regexp)
主要懂3个mysql的方法:replace[替换] regexp[正则匹配] concat[连接] 由于某些原因,有时候我们没有按照范式的设计准则而把一些属性放到同一个字符串字段中.比如 ...
- Lua笔记
闭包 示例一 function newCounter() return function() -- anonymous function i = i + return i end end c1 = n ...
- Codeforces 721E Road to Home
题意 输入第一行有4个数,分别为\(L,n,p,t\),分别表示总长度为\(L\)的路,中间有\(n\)个互不相交的区间,现在要用长度为\(p\)的小木棒从左往右铺路(木棒不能被折断,也不能有重叠,且 ...
- penpyxl basic function demo code
Openpyxl basic function demo code demo code: #!/usr/bin/env python # -*- coding: utf-8 -*- "&qu ...
- 二、JSP、servlet、SQL三者之间的数据传递(前台与后台数据交互)
2.收信息来到表单提交时URL所指向的servlet文件,获取传递过来的参数值
- ubuntu 16.04 安装完QQ后,更新或apt-get报错
apt-get install python-pip正在读取软件包列表... 完成正在分析软件包的依赖关系树 正在读取状态信息... 完成 您可能需要运行“apt-get -f install”来纠正 ...