/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *p=head,*newHead=NULL,*next=NULL;
while(p){
next = p->next;
p->next = newHead;
newHead = p;
p = next;
}
return newHead;
}
};
 
Reverse Linked List II
Total Accepted: 58179 Total Submissions: 218404 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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode *head)
{
ListNode* p = head,*newHead = NULL,*next = NULL;
while(p){
next = p->next;
p->next = newHead;
newHead = p;
p = next;
}
return newHead;
}
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(m==n){
return head;
}
ListNode *m_pre=NULL,*n_next=NULL,*tail=NULL,*cur=head;
int cnt = ;
while(cur){//找m的前驱
++cnt;
if(cnt == m){
tail = cur;//反转之前的头,反转之后的尾
break;
}
m_pre = cur;
cur=cur->next;
}
while(cur){//找n的后继
n_next = cur->next;
if(cnt == n){
break;
}
cur = n_next;
++cnt;
}
if(cur){
cur->next = NULL;
}
ListNode *newHead = reverseList(tail);
if(m_pre){
m_pre->next = newHead;
}else{
head = newHead==NULL? head:newHead;
}
if(tail){
tail->next = n_next;
}
return head;
}
};

[Linked List]Reverse Linked List,Reverse Linked List II的更多相关文章

  1. REVERSE关键字之REVERSE索引

    昨天说到REVERSE关键字可以指REVERSE函数和REVERSE索引,简单介绍了下REVERSE函数的含义,今天简单整理下REVERSE索引. REVERSE索引也是一种B树索引,但它物理上将按照 ...

  2. [Linked List]Delete Node in a Linked List

    otal Accepted: 48115 Total Submissions: 109291 Difficulty: Easy Write a function to delete a node (e ...

  3. Linked List-237. Delete Node in a Linked List

    Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...

  4. [LeetCode] 344 Reverse String && 541 Reverse String II

    原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...

  5. (reverse) Text Reverse hdu1062

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  6. 攻防世界 reverse 进阶 10 Reverse Box

    攻防世界中此题信息未给全,题目来源为[TWCTF-2016:Reverse] Reverse Box 网上有很多wp是使用gdb脚本,这里找到一个本地还原关键算法,然后再爆破的 https://www ...

  7. [LeetCode] Reverse Linked List

    Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...

  8. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

  9. Reverse Linked List I&&II——数据结构课上的一道题(经典必做题)

    Reverse Linked List I Question Solution Reverse a singly linked list. Reverse Linked List I 设置三个指针即可 ...

  10. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

随机推荐

  1. C# ?? 操作符示例

    static int? GetNullableInt() { return null; } static string GetStringValue() { return null; } static ...

  2. Properties的读取和写入

    Properties是HashTable下的一个持久的属性集,没有泛型,key-value都是String类型.由于能与IO流结合使用,所以能方便地操作属性文件或者xml文件. 一.propertie ...

  3. [转载]Matlab中fft与fftshift命令的小结与分析

    http://blog.sina.com.cn/s/blog_68f3a4510100qvp1.html 注:转载请注明出处——by author. 我们知道Fourier分析是信号处理里很重要的技术 ...

  4. 【.NetRemoting-2】2015.09.17

    [Remoting架构] [1]是.NetFramework的一个重要组成 [2]框架的两个重要特性 [A]基本实现[B]可扩展/可定制 [各个组成部分] [1][客户端,客户端应用程序域] [组成] ...

  5. bootstrap之 formgroup表单布局样式

    <form class="form-horizontal" role="form"> <fieldset> <legend> ...

  6. IE的documentMode属性

    参看下面链接:<IE的documentModeshuxing>

  7. yii操作数据库(AR)

    模型: 有多少数据表,就建立多少模型 模型其实就是类 我们对数据库进行操作,需要实例化模型类,产生对象 通过对象调用相关的方法,就可以实现数据库的操作   增加记录 [php] $post =newP ...

  8. php 支持递归函数.递归函数就是调用函数本身.

    例子 将一个字符进行颠倒 function reverse_r($str){ if(strlen($str)){ reverse_r(substr($str,1));// } echo substr( ...

  9. Spring 系列: Spring 框架简介(转载)

    Spring 系列: Spring 框架简介 http://www.ibm.com/developerworks/cn/java/wa-spring1/ Spring AOP 和 IOC 容器入门 在 ...

  10. 初学swift笔记 继承(十)

    import Foundation /* 继承 class childClass: FatherClass{ } 优点 代码重用 缺点 增加程序的藕合度 父类的改变会影响子类 只能单继承 */ cla ...