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)的更多相关文章

  1. 【LeetCode】Swap Nodes in Pairs 链表指针的应用

    题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...

  2. 【LeetCode】Swap Nodes in Pairs 解题报告

    Swap Nodes in Pairs [LeetCode] https://leetcode.com/problems/swap-nodes-in-pairs/ Total Accepted: 95 ...

  3. 【LeetCode】Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  4. 【链表】Swap Nodes in Pairs(三指针)

    题目: Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1-> ...

  5. 【LeetCode】Swap Nodes in Pairs(两两交换链表中的节点)

    这是LeetCode里的第24题. 题目要求: 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 示例: 给定1->2->3->4, 你应该返回2->1->4- ...

  6. 【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 ...

  7. leetcode 之Swap Nodes in Pairs(21)

    不允许通过值来交换,在更新指针时需要小心. ListNode *swapNodes(ListNode* head) { ListNode dummy(-); dummy.next = head; fo ...

  8. 【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 ...

  9. 【leetcode】Search for a Range(middle)

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. 电脑开机黑屏,显示Reboot and Select proper boot device!

    “reboot and select proper boot device or insert boot media in selected boot device and press a key” ...

  2. jquery隐藏按钮

    $(function () { jhbs = getQueryStringByName('jhbs'); shhbs = getQueryStringByName('shhbs'); if (shhb ...

  3. ggplo2学习笔记——基本图形类型

    1.散点图:又称散点分布图,是以一个变量为恨坐标,另一个变量为纵坐标,利用散点(坐标点)的分布形态反映变量统计关系的一种图形.可以用来确认两个变量之间的关系.绘制自由曲线.矩阵关联分析等. 2.条形图 ...

  4. 新年新技术:HTTP/2

    新的一年,项目也要带着发展的眼光往前走,得跟上潮流,当然前提是自己真的用的上. 用的上用不上都得先简单了解下. 2月下旬Google发布了首个基于HTTP/2的RPC框架GRPC,它是基于HTTP/2 ...

  5. 【C语言入门教程】7.4 共用体

    7.4 共用体 共用体又称为联合体,是由不同的数据类型组成的一个整体.与结构体不同的是,共用体每次只能使用其中一个成员.结构体的总长度是结构体所有成员长度之和,共用体的总长度是其中最长一个数据类型的长 ...

  6. php 通过curl post发送json数据实例

    例1  代码如下 复制代码 $data = array("name" => "Hagrid", "age" => "3 ...

  7. JQuery实战手风琴-遁地龙卷风

    (-1)写在前面 这个图片是我从网上下载的,向这位前辈致敬.图片资源在我的百度云盘里.http://pan.baidu.com/s/1nvfJHdZ 我用的是chrome49,JQuery3.0,案例 ...

  8. 如何在IE8设置透明背景

    background:rgba(0,0,0,0.5);filter: progid:DXImageTransform.Microsoft.gradient(startcolorstr=#7F00000 ...

  9. OpenCv高斯,中值,均值,双边滤波

    #include "cv.h" #include "highgui.h" #include <iostream> using namespace s ...

  10. 2016-03-12 Leanning Plan

    1,Python 2,Java 3,Html+Css 4,PHP 5,Crawl 6,WetChat Platform