【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 ...
随机推荐
- SQLite初试...
string dataPath = "../../UserData.dbx"; //System.IO.Directory.GetCurrentDirectory() + &quo ...
- IIS 您要访问的网页有问题,无法显示!
提示:您要访问的网页有问题,无法显示.HTTP 500 – 内部服务器错误.”的问题解决方案! IIS服务器出现错误的原因很多,请尝试以下操作:1.查看网站属性——文档看看启用默认文档中是否存在:in ...
- java httpclient发送json 请求 ,go服务端接收
/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...
- QT中Sqlite的使用
环境: 静态编译过sqlite 步骤: 1.C++链接器中加入Sqlite.lib,然后在测试一下是否能正常加载Sqlite驱动 #include<QtPlugin> Q_IMPORT_P ...
- HDOJ 4734 F(x)
数位DP.... F(x) Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Java项目相关监控与调优
Linux JVM Tomcat =========Linux =============== 监控 nmon 命令:nmon -s 10 -c 60 -f -m /home -s 10 每10s ...
- 学习javascript系列之变量
在javascript全局变量中,未加var声明的全局变量和加上var声明的全局变量是不同的,虽然都是window对象的属性. ; window.a //1 delete a //false; 通过v ...
- python 列表推导式
squares = [x**2 for x in range(10)] 相当于squares = map(lambda x: x**2, range(10)),但是更简洁和易读.傻逼才会用最古老的fo ...
- 如何优雅地使用 Sublime Text
Sublime Text:一款具有代码高亮.语法提示.自动完成且反应快速的编辑器软件,不仅具有华丽的界面,还支持插件扩展机制,用她来写代码,绝对是一种享受.相比 于难于上手的Vim,浮肿沉重的Ecli ...
- caffe学习系列(6):其他层介绍
主要包括softmax-loss层(与softmax有区别),全连接层(Inner Prouduct),accuracy层,reshape层, Dropout层. softmax: layers { ...