【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 ...
随机推荐
- C++你不知道的那些事儿—C++语言的15个晦涩特性
这个列表收集了 C++ 语言的一些晦涩(Obscure)特性,是我经年累月研究这门语言的各个方面收集起来的.C++非常庞大,我总是能学到一些新知识.即使你对C++已了如指掌,也希望你能从列表中学到一些 ...
- JS keycode 事件响应
<script language="javascript"> function keyevent(){ if(event.keyCode==13) alert(&quo ...
- Python XML解析(转载)
Python XML解析 什么是XML? XML 指可扩展标记语言(eXtensible Markup Language). 你可以通过本站学习XML教程 XML 被设计用来传输和存储数据. XML是 ...
- lustre文件系统部署流程
# 1 准备工作### 1.1 添加以太网址添加以太网地址,使得gio017可以访问到需要安装的节点.修改gio017上的/etc/hosts,将需要批量操作的节点名以如下方式添加.```[gio01 ...
- TP3.1 中URL和APP区别
1.__URL__指当前模块地址,就是当前的action的地址.(每个__action都是一个模块) eg:当前打开config.html,那么config.html里边的__URL__/sav ...
- js null和undefined
在JavaScript开发中,被人问到:null与undefined到底有啥区别? 一时间不好回答,特别是undefined,因为这涉及到undefined的实现原理. 总所周知:null == un ...
- PHP Socket实现websocket(四)Select函数
int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); /* ...
- POJ1011
今天搞了一下传说中的经典搜索题——poj1011,果然里面充斥着各种巧妙的剪枝,做完之后回味一下还是感觉构思太巧妙,所以总结记录一下加深理解. 原题:http://poj.org/problem?id ...
- html5拖拽实现
1.需求 做一个h5正方形的拖拽框 2.分析 使用touchstart,touchmove,touchend这3个事件实现. 需要记录的数据有三组数据,分别是下图的(x0,y0),(x1,y1),(x ...
- JSON字符串转JavaBean,net.sf.ezmorph.bean.MorphDynaBean cannot be cast to ……
在json字符串转java bean时,一般的对象,可以直接转,如:一个学生类,属性有姓名.年龄等 public class Student implements java.io.Serializab ...