LeetCode 92. ReverseLinkedII
#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) { ListNode *dummy = new ListNode(), *pre = dummy, *cur;
dummy -> next = head;
for (int i = ; i < m - ; i++) {
pre = pre -> next;
}
cur = pre -> next;
for (int i = ; i < n - m; i++) {
ListNode* temp = pre -> next;
pre -> next = cur -> next;
cur -> next = cur -> next -> next;
pre -> next -> next = temp;
}
return dummy -> next;
}
}; void test_data()
{
ListNode *head = new ListNode();
ListNode *p ;
p = head;
Solution s;
int n = ;
int T = ;
while (T-- && cin >> n)
{
ListNode *q;
q = new ListNode(n);
p->next = q;
p = q;
}
p->next = NULL; head = head->next;
s.reverseBetween(head, , );
while (head)
{
cout << head->val << " ";
head = head->next; }
}
int main()
{
test_data();
return ; }
LeetCode 92. ReverseLinkedII的更多相关文章
- LeetCode 92 | 大公司常考的面试题,翻转链表当中指定部分
今天是LeetCode专题的第58篇文章,我们一起来看看LeetCode 92题,翻转链表II(Reverse LInked List II). 这题的官方难度是Medium,2451个赞同,145个 ...
- [LeetCode] 92. Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- LeetCode 92. 反转链表 II(Reverse Linked List II)
92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...
- [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- Leetcode 92.反转链表
92.反转链表 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL ...
- [LeetCode] 92. Reverse Linked List II 反向链表II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- Java实现 LeetCode 92 反转链表 II
92. 反转链表 II 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5-> ...
- leetcode 92 Reverse Linked List II ----- java
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- Leetcode#92 Reverse Linked List II
原题地址 第一步,找到将要翻转的位置,记录翻转部分前一个节点(prev) 第二步,翻转,记录翻转完成后这部分的首(reverseHead)和尾(reverseTail),以及翻转部分之后的一个节点(p ...
随机推荐
- 一道C++、MFC上机面试题
题目:写一个基于MFC对话框的程序,界面输入整型a和b,点击计算,开启线程计算a+b,并把结果返回给对话框.(1)不能用结构体和类(2)用到自定义消息(3)鼠标移到[计算]按钮上变为收尸图标.参考界面 ...
- qt 打包发布 获取dll
发布前,获取所有qt dll包命令 生成的程序运行正常之后,找到项目的生成目录,比如 项目源码路径: C:\QtPros\hellomw\它的项目生成目录是C:\QtPros\build-hellom ...
- 我的第一个python web开发框架(27)——定制ORM(三)
在上一章中,我们已经创建好ORM的基类了,接下来要做的就是将基类的常用方法一一实现. 首先我们来看看之前项目中,最常见的获取指定主键的记录实体 @get('/api/product/<id:in ...
- About Pull Strings 英语走后门议论文
About pull strings Author : Pleiades_Antares 1. From ancient times to the present, the "going b ...
- bootatrsp datetimepicker的初始化和阻止模态窗关闭(事件冒泡)
1.github下载资源包 http://www.bootcss.com/p/bootstrap-datetimepicker/ 2.引入bootstrap-datetimepicker.min.c ...
- 【Python 10】汇率兑换3.0(while循环)
1.案例描述 设计一个汇率换算程序,其功能是将美元换算成人民币,或者相反. 2.0增加功能:根据输入判断是人民币还是美元,进行相应的转换计算 3.0增加功能:程序可以一直运行,知道用户选择退出 2.案 ...
- 运行ConnectionDemo时遇到的问题及解决方案
20175227张雪莹 2018-2019-2 <Java程序设计> 运行ConnectionDemo时遇到的问题及解决方案 老师博客上提供确认数据库连接的代码 import static ...
- [APIO2014]序列分割
嘟嘟嘟 复习一下斜率优化,感觉已经忘得差不多了-- 这题切入点在与答案跟切的顺序无关. 证明就是假如有三段权值分别为\(x, y, z\),那么这两刀不管按什么顺序切,得到的结果都是\(xy + xz ...
- Linux后台命令的使用说明
1)ctrl+Z:停止当前进程 首先先将一个程序运行起来,这个时候如果你需要去干别的事情,需要暂停运行,可以使用ctrl+Z: user@mine:/opt/user/pytorch-gender$ ...
- Spring Security(三十七):Part IV. Web Application Security
Most Spring Security users will be using the framework in applications which make user of HTTP and t ...