Leetcode#92 Reverse Linked List II
第一步,找到将要翻转的位置,记录翻转部分前一个节点(prev)
第二步,翻转,记录翻转完成后这部分的首(reverseHead)和尾(reverseTail),以及翻转部分之后的一个节点(post)
第三部,将prev、reverseHead、reverseTail、post连接起来
代码:
ListNode *reverseBetween(ListNode *head, int m, int n) {
ListNode *node = head;
ListNode *prev = NULL;
ListNode *post = NULL;
ListNode *reverseHead = NULL;
ListNode *reverseTail = NULL;
int count = ;
if (!head)
return NULL;
prev = NULL;
while (node && count < m ) {
prev = node;
node = node->next;
count++;
}
if (!node)
return head;
reverseTail = node;
post = node->next;
while (post && count < n) {
ListNode *postNext = post->next;
post->next = node;
node = post;
post = postNext;
count++;
}
reverseHead = node;
reverseTail->next = post;
if (!prev)
return reverseHead;
else {
prev->next = reverseHead;
return head;
}
}
Leetcode#92 Reverse Linked List II的更多相关文章
- [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-> ...
- [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 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倒置链表2 C++
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- [leetcode]92. Reverse Linked List II反转链表2
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- [LeetCode 92] Reverse Linked List II 翻转单链表II
对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...
- [LeetCode]92. Reverse Linked List II反转部分链表
/* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 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- ...
随机推荐
- 《Google 代码风格指南》
<Google 代码风格指南> https://github.com/google/styleguide
- 用开源中国(oschina)Git管理代码(整合IntelliJ 13.1.5)
简介 开源中国提供了Git服务(地址:http://git.oschina.net/),在速度上比国外的github要快很多.使用了一段时间,感觉很不错.oschina git提供了演示平台,可以运行 ...
- Android四大组件一----Activity
最新面试需要复习一下Android基础. {所谓Activity} 通俗点:app上看到的窗口基本都是Activity Android 程序一般是由多个Activity组成,用户看到的能够交互的窗口通 ...
- ROS 端口IP映射 动态IP映射
chain=dstnat action=dst-nat to-addresses= protocol=tcp dst-address-type=local dst-port= log=no log-p ...
- How to setup and process Intercompany accounting [AX2012]
In this post, I will take you through a very simple functionality called the intercompany accountin ...
- pure css做的pc登陆界面
源码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...
- [转]C++编写Config类读取配置文件
//Config.h #pragma once #include <string> #include <map> #include <iostream> #incl ...
- Java实现多线程邮件发送
利用java多线程技术配合线程池实现多任务邮件发送. 1.基本邮件发送MailSender package hk.buttonwood.ops.email; import java.io.File; ...
- Android--获取使用的总流量和每个App的上传、下载的流量
获得每个App的上传.下载的流量. 思路就是获取到我们手机上的所有app,再获得app里面使用的权限,如果app有网络权限,就显示出来. 代码很简单,代码里面也有比较详细的注释,下面直接上代码 布局文 ...
- Windows Phone Emoji
今天基于项目的需要,研究了一下windows phone 8里面的Emoji实现.如果大家用过wp版本的微信或者qq,相比一定对它里面的表情符号影像深刻吧!是的,只要你细看一下,其实在微信里面包括两种 ...