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 ...
随机推荐
- logstash收集的日志输出到elasticsearch中
logstash收集的日志输出到elasticsearch中 一.需求 二.实现步骤 1.编写pipeline文件 1.`elasticsearch`配置参数解析: 2.可能会报的一个异常 2.准备测 ...
- java监控JVM的内存使用情况等
以下的程序监控参数的代码,有些是从网络上获取的,此处进行一个记录是为了以后如果要用到方便记录. 1.引入jar包,为了获取一些cpu的使用率等信息 <dependency> <gro ...
- python3.5 安装mysqlclient
python 3.5 安装 mysqlclient 会失败 pip install mysqlclient 注意这里环境中只有python3.5 会出现一大堆红字 编译终止, error: comma ...
- 2021 ICPC Gran Premio de Mexico 2da Fecha部分题题解
前面的水题,在队友的配合下,很快就拿下了,剩下几道大毒瘤题,一直罚座三个小时,好让人自闭...但不得不说,这些题的质量是真的高! H. Haunted House 首先看这个题,大眼一扫,觉得是某种数 ...
- 第05课 OpenGL 3D空间
3D空间: 我们使用多边形和四边形创建3D物体,在这一课里,我们把三角形变为立体的金子塔形状,把四边形变为立方体. 在上节课的内容上作些扩展,我们现在开始生成真正的3D对象,而不是象前两节课中那样3D ...
- Go语言核心36讲(Go语言进阶技术十三)--学习笔记
19 | 错误处理(上) 提到 Go 语言中的错误处理,我们其实已经在前面接触过几次了. 比如,我们声明过error类型的变量err,也调用过errors包中的New函数. 我们说过error类型其实 ...
- 转向系统的传递路径分析(Transfer Path Analysis)入门的一些分享
分享一些自己对于<转向系统><传递路径分析>的理解 (只是一些个人理解,不涉及任何公司隐私问题,logo就懒得一个个去擦了) (1) (2) (3) (4) (5) (6) ( ...
- 攻防世界 Misc 新手练习区 ext3 bugku Writeup
攻防世界 Misc 新手练习区 ext3 bugku Writeup 题目介绍 题目考点 WinHex工具的使用 linux磁盘挂载mount命令 Writeup 下载附件拖进winhex分析一下,查 ...
- 暑假算法练习Day3
第三天!!!最近要开始归纳总结Python学习啦!! 1006 换个格式输出整数 (15 分) 让我们用字母 B 来表示"百".字母 S 表示"十",用 12. ...
- MySQL基础语句(MySQL内置函数 )
MySQL 字符串函数 函数 描述 实例 ASCII(s) 返回字符串 s 的第一个字符的 ASCII 码. 返回 CustomerName 字段第一个字母的 ASCII 码: SELECT ASCI ...