Leetcode 92
/**
* 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) {
ListNode* pre = new ListNode(-);
pre->next = head;
ListNode* move = pre;
for(int i=;i < m;i++){
move = move->next;
}
ListNode* connect1 = move;
ListNode* head1 = move->next; for(int i=;i <= n-m;i++){
move = move->next;
}
ListNode* head2 = move->next;
move->next = NULL; head1 = func(head1);
connect1->next = head1;
while(head1->next != NULL){ head1 = head1->next;} head1->next = head2; return pre->next;
} ListNode* func(ListNode* head){
ListNode* pre = new ListNode(-);
ListNode* move = head;
while(move != NULL){
move = move->next;
head->next = pre->next;
pre->next = head;
head = move;
}
return pre->next;
}
};
Leetcode 92的更多相关文章
- 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 ...
- LeetCode 92. ReverseLinkedII
#include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(i ...
随机推荐
- ES6学习--Object.assign()
ES6提供了Object.assign(),用于合并/复制对象的属性. Object.assign(target, source_1, ..., source_n) 1. 初始化对象属性 构造器正是为 ...
- MHA集群(gtid复制)和vip漂移
在上一片博客中,讲述了怎么去配置MHA架构!这片博客不再细说,只说明其中MySQL主从搭建,这里使用的是gtid加上半同步复制! 步骤与上一片博客一样,不同之处在于MySQL主从的搭建!详细的gtid ...
- c++性能之对象与指针性能比较、以及java与c++性能对比实测
为了更加直观的比较,好吧,我们选择以对象的初始化并add到list为例子. 首先,定义object如下: #include <string> #pragma once using name ...
- C++设计模式 之 “对象创建”模式:Factory Method、Abstract Factory、Prototype、Builder
part 0 “对象创建”模式 通过“对象创建” 模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定.它是接口抽象之后的第一步工作. 典型模式 Fact ...
- zabbix-server新增zabbix-agent
zabbix监控系统搭建好了之后,就需要为各种角色host加入进来,现在新增一台zabbix-agent: 1.在172.16.23.128上安装zabbix-agent,zabbix-server: ...
- Python3基础 issubclass 判断基类
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Specify Computed Columns in a Table
https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table ...
- js自动小轮播
使用定时器,换个图片地址. 从1到5换,如果大于5了再跳到1,点击停止时关闭定时器. <!-- Author: XiaoWen Create a file: 2016-12-08 13:19:2 ...
- HDU 6072 Logical Chain(Kosaraju+bitset)
http://acm.hdu.edu.cn/showproblem.php?pid=6072 题意: 给你$n*n$的矩阵,每次修改k条边,让你计算其中能相互到达的点对有多少. 思路: 其实就是求强连 ...
- ubuntu 14.04 添加、删除用户,修改用户名称,修改主机名
ubuntu添加用户 sudo adduser test ubuntu删除用户 sudo userdel test cd /home/ sudo rm -r test