49-Reverse Linked List II
- Reverse Linked List II My Submissions QuestionEditorial Solution
Total Accepted: 70579 Total Submissions: 252986 Difficulty: Medium
Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
思路:
时间复杂度:O(n),空间复杂度O(1)
找到指定段元素,对该段元素逆转,同时保留指定段的两端的邻居
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
assert(n>=m);
if(m==n)return head;
int i=1,j=1;
ListNode *p=head,*q=head,*prep=NULL;//保留指定段首元素的前一元素
while(p!=NULL&&i++<m){
if(i==m)prep=p;
p=p->next;
}
while(q!=NULL&&j++<n)q=q->next;/找到各自的最终位置,越界为空
if(p==NULL)return head; //起始位置越界,直接返回head
ListNode * tmp = p->next,*prenode = p,*qnext=q->next;
while(tmp!=qnext) //还未到达指定段的下一元素,则当前元素指向前一元素
{
ListNode *nextnode = tmp->next;
tmp->next = prenode;
prenode = tmp;
tmp = nextnode;
}
p->next = qnext;
if(prep!=NULL)prep->next = prenode;
else head = prenode;
return head;
}
};
49-Reverse Linked List II的更多相关文章
- 【leetcode】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 14. Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)
题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...
- 92. Reverse Linked List II
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1- ...
- lc面试准备:Reverse Linked List II
1 题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1 ...
- 【LeetCode练习题】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- [Linked List]Reverse Linked List,Reverse Linked List II
一.Reverse Linked List (M) Reverse Linked List II (M) Binary Tree Upside Down (E) Palindrome Linked ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- leetcode -day30 Reverse Linked List II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one- ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
随机推荐
- 脚本:bat实现自动转换windows远程端口
问题描述:通过一个脚本可以实现windows远程端口的转换,这个是拷贝过来学习的一个脚本 @echo off color f0 echo 修改远程桌面3389端口(支持Windows 2003 200 ...
- 学习手册 | MySQL篇 · 其一
InnoDB关键特性 插入缓冲(Insert Buffer) 问题: 在InnoDB插入的时候,由于记录通常都是按照插入顺序,也就是主键的顺序进行插入的,因此,插入聚集索引是顺序的,不需要随机IO ...
- C++的指针使用心得
使用C++有一段时间了,C++的手动内存管理缺失很麻烦,一不小心容易产生内存泄漏.自己总结了一点使用原则(不一定对),备注一下,避免忘记. 1.类外部传来的指针不处理 2.Qt对象管理的内存不处理 3 ...
- 查找最小生成树:克鲁斯克尔算法(Kruskal)算法
一.算法介绍 Kruskal算法是一种用来查找最小生成树的算法,由Joseph Kruskal在1956年发表.用来解决同样问题的还有Prim算法和Boruvka算法等.三种算法都是贪心算法的应用.和 ...
- Python matplotlib 概率论与数理统计 伯努利分布 二项分布
Python 代码实现 二项分布 import numpy as np import matplotlib.pyplot as plt import math from scipy import st ...
- hdu 5172 GTY's gay friends(线段树最值)
题意: GTY有n个朋友,站成一排,每个人有一个特征值ai. 有m个询问.每次询问给两个数L,R.问你[L,R](即aL...aR)是否是1..(R-L+1)的一个全排列. 是输出YES,否则输出NO ...
- JAVA笔记12__字节、字符缓冲流/打印流/对象流/
/** * !!:以后写流的时候一定要加入缓冲!! * 对文件或其它目标频繁的读写操作,效率低,性能差. * 缓冲流:好处是能更高效地读写信息,原理是将数据先缓冲起来,然后一起写入或读取出来. * * ...
- Harbor仓库搭建及使用
目录 一.docker配置 二.安装docker-compose 三.安装harbor 四.管理harbor 五.springboot项目配置docker 六.linux服务器上打包并推送至harbo ...
- 云主机新增swap分区
云主机内存不够用,如何新增swap 分区. 如何启用swap分区? 步骤如下: 1.查看当前系统中是否已经启用swap分区 1 2 cat /proc/swaps top 2.如果没有启用swa ...
- properties 文件解析
1.提供properties文件 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/future?useUn ...