【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 ...
随机推荐
- gulp进阶构建项目由浅入深
gulp进阶构建项目由浅入深 阅读目录 gulp基本安装和使用 gulp API介绍 Gulp.src(globs[,options]) gulp.dest(path[,options]) gulp. ...
- 如何让 height:100%; 起作用
当你设置一个页面元素的高度(height)为100%时,期望这样元素能撑满整个浏览器窗口的高度,但大多数情况下,这样的做法没有任何效果.你知道为什么height:100%不起作用吗? 按常理,当我们用 ...
- 【转】GATK使用方法详解(包含bwa使用)
一.使用GATK前须知事项: (1)对GATK的测试主要使用的是人类全基因组和外显子组的测序数据,而且全部是基于illumina数据格式,目前还没有提供其他格式文件(如Ion Torrent)或者实验 ...
- HDOJ 1561 The more, The Better
树形DP.... The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- git之tag
参考:git命令之git tag 给当前分支打标签 1.git tag //查看已有标签 2.创建本地标签 git tag tag_name //创建标签 git tag -a v0.1.2 -m ...
- php网络编程
php面试题之三--PHP网络编程(高级部分) 三.PHP网络编程 [!!!]1.禁用COOKIE后SEESION还能用吗?(51.com笔试题) 可以,COOKIE和SESSION都是用来实现会话机 ...
- ORA-14402: 更新分区关键字列将导致分区的更改
默认情况下,oracle的分区表对于分区字段是不允许进行update操作的,如果有对分区字段行进update,就会报错——ORA-14402: 更新分区关键字列将导致分区的更改.這種情況可以通過開啟表 ...
- iOS设计中的“代理”
“代理”--在iOS的开发设计中是一个非常重要的概念,同时又是十分基础的知识.所以,掌握“代理”势在必行! 以下,结合一个具体的例子,详细认识“代理”: 1, 图例解释: ①:定义两个文本输入框UIT ...
- 如何用C语言编写病毒‘
怎样用C语言编写病毒在分析病毒机理的基础上,用C语言写了一个小病毒作为实例,用TURBOC2.0实现.[Abstract] This paper introduce the charateristic ...
- 跟着百度学PHP[4]OOP面对对象编程-8-继承
如下图所示.人就是父类!而NBA球员以及女主播就是子类 要继承一个类,那么在类名的后面加上extends 要继承的类名 具体格式:class Student extends human{} # ...