【leetcode】Swap Nodes in Pairs (middle)
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.
//start time = 9:54
//end time = 10:16
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *swapPairs(ListNode *head) {
if(head == NULL || head->next == NULL) return head;
//头指针转换
ListNode * newhead = head->next;
head->next = newhead->next;
newhead->next = head; ListNode * pre = newhead->next; //之前转换完的最后一个
ListNode * cur = NULL; //一对中的前一个
ListNode * next = NULL;//一对中的后一个
while(pre->next != NULL && pre->next->next != NULL)
{
cur = pre->next;
next = cur->next;
pre->next = next;
cur->next = next->next;
next->next = cur; pre = cur;
} return newhead;
}
};
发现多出了c的选项 用c写的时候 ListNode前面要加 struct 修饰 而C++不用 注意区别 时间上C需要1ms 而C++需要6ms
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *swapPairs(struct ListNode *head) {
if(head == NULL || head->next == NULL) return head;
//头指针转换
struct ListNode * newhead = head->next;
head->next = newhead->next;
newhead->next = head; struct ListNode * pre = newhead->next; //之前转换完的最后一个
struct ListNode * cur = NULL; //一对中的前一个
struct ListNode * next = NULL;//一对中的后一个
while(pre->next != NULL && pre->next->next != NULL)
{
cur = pre->next;
next = cur->next;
pre->next = next;
cur->next = next->next;
next->next = cur; pre = cur;
} return newhead;
}
【leetcode】Swap Nodes in Pairs (middle)的更多相关文章
- 【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】Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- 【链表】Swap Nodes in Pairs(三指针)
题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...
- 【LeetCode】Swap Nodes in Pairs(两两交换链表中的节点)
这是LeetCode里的第24题. 题目要求: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定1->2->3->4, 你应该返回2->1->4- ...
- 【leetcode】Reverse Nodes in k-Group (hard)☆
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...
- leetcode 之Swap Nodes in Pairs(21)
不允许通过值来交换,在更新指针时需要小心. ListNode *swapNodes(ListNode* head) { ListNode dummy(-); dummy.next = head; fo ...
- 【leetcode】Validate Binary Search Tree(middle)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【leetcode】Search for a Range(middle)
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- [PHP知识点乱炖]四、全局变量——小偷从良记
本章要讲的是PHP的全局变量. 这里讲个小故事: 很多年前,一个很聪明的小偷,想去偷一户人家的钱.可是他偷不到主人的钥匙,怎么办呢? 他想到了一个办法,去之前嚼了一块口香糖,口香糖的牌子是“大大泡泡糖 ...
- 通过Canvas及File API缩放并上传图片完整示例
<!DOCTYPE html> <html> <head> <title>通过Canvas及File API缩放并上传图片</title> ...
- [译]AngularJS Service vs Factory - Once and for all
原文: http://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-and-for-all.html Service和Fa ...
- [译]angularjs directive design made easy
原文: http://seanhess.github.io/2013/10/14/angularjs-directive-design.html AngularJS directives很酷 Angu ...
- PHP的 Mysqli扩展库的多语句执行
$mysqli->multi_query($sqls); 执行多个sql语句,返回true/false 有结果集时,使用 $mysqli->store_result(); 来获取结 ...
- 微信小程序开放公测了 晚上又可以通宵搞代码了
就在刚刚22:15分,微信公众平台的服务号发来好消息说小程序开放公测了,喜大普奔啊!!!码农们晚上又可以通宵搞测试了.之前还猜测16日微信小论坛小程序专场上会公布,没想到提前了那么多天,效率挺高的,而 ...
- Hadoop 面试题之storm 3个
Hadoop 面试题之八 355.metaq 消息队列 zookeeper 集群 storm集群(包括 zeromq,jzmq,和 storm 本身)就可以完成对商城推荐系统功能吗?还有其他的中间件? ...
- Mysql BLOB和TEXT类型
BLOB是一个二进制大对象,可以容纳可变数量的数据.有4种BLOB类型:TINYBLOB.BLOB.MEDIUMBLOB和LONGBLOB.它们只是可容纳值的最大长度不同. A binary la ...
- H5图像遮罩-遁地龙卷风
(-1)写在前面 这个idea不是我的,向这位前辈致敬.我用的是chrome49.用到的图片资源在我的百度云盘里http://yun.baidu.com/share/link?shareid=1970 ...
- 小米手机无法打开程序报错Unable to instantiate application com.android.tools.fd.runtime.BootstrapApplication的解决办法
打开studio的setting 然后 Preferences -> Build, Execution, Deployment -> Instant Run -> Enable In ...