The task is reversing a list in range m to n(92) or a whole list(206).

  All in one : U need three pointers to achieve this goal.

   1) Pointer to last value

   2) Pointer to cur p value

   3) Pointer to next value

  Here, showing my code wishes can help u.

  Of course, you also need to record four nodes in special postions.

   1) newM  2)newN  3)beforeM  4)afterN

  These may be some complex(stupid) but it's really friend to people who are reading my code and easily understood.

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; typedef struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}ListNode, *PNode; void create_List(PNode head)
{
PNode p = head;
int n;
cin>>n;
for(int i = ; i < n ;i ++){
int t;
cin>>t;
if(i == ){
head -> val = t;
head -> next = NULL;
cout<<"head is "<<head->val<<endl;
p = head;
}else{
PNode newNode = (PNode) malloc(sizeof(PNode));
newNode -> val = t;
newNode -> next = NULL;
p -> next = newNode;
p = newNode;
cout<<"p is "<<p -> val<<endl;
}
}
} void display(PNode head)
{
PNode p = head;
while(p){
cout<<p->val<<" -> ";
p = p -> next;
}cout<<endl;
} class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(m == n || head == NULL) return head;
ListNode *pLast = head, *p = head -> next, *pNext = NULL;
ListNode *newN = NULL, *newM = NULL, *beforeM = head, *afterN = NULL;
int pos = ;
while(p){
if(pos == m - ){
beforeM = pLast;
pLast = p;
p = p -> next;
}
else if(pos >= m && pos < n){
pNext = p -> next;
p -> next = pLast;
if(pos == m){
pLast -> next = NULL;
newM = pLast;
}
pLast = p;
if(pos == n - ){
newN = p;
afterN = pNext;
}
p = pNext;
}else{
pLast = p;
p = p -> next;
}
pos ++;
}
if( m== && afterN == NULL){
head = newN;
}else if(m == ){
head = newN;
newM -> next = afterN;
}else{
beforeM -> next = newN;
newM -> next = afterN;
}
return head;
} ListNode* reverseList(ListNode* head) {
if(head == NULL) return head;
ListNode *pLast = head, *p = head -> next, *pNext = NULL;
while(p){
pNext = p -> next;
p -> next = pLast;
if(pLast == head){
pLast -> next = NULL;
}
pLast = p;
p = pNext;
}
head = pLast;
return head;
}
};
int main()
{
PNode head = (PNode) malloc(sizeof(PNode));;
create_List(head);
cout<<"after creating , head is "<<head->val<<endl;
display(head);
Solution tmp = Solution();
//tmp.reverseBetween(head, 2, 3);
tmp.reverseList(head);
system("pause");
return ;
}

【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List的更多相关文章

  1. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  2. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  4. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  5. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  6. 【leetcode】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-> ...

  7. 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...

  8. 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...

  9. 【leetcode】Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

随机推荐

  1. 洛谷 2055 BZOJ 1433 [ZJOI2009]假期的宿舍

    [题解] 既然是一人对应一床,那么显然可以用二分图匹配来做.俩人认识的话,如果其中一个a是在校学生,另一个b不回家,b就可以向a的床连边(a,b当然也可以是同一个人). 然后如果最大匹配数大于等于需要 ...

  2. IOC控制反转之Autofac

    https://www.jianshu.com/p/1b6cb076e2e5 博主:衡泽_徐峰 Autofac官网:https://autofac.org/ Autofac 是.Net非常好的一个IO ...

  3. zoj 3768 Continuous Login

    Pierre is recently obsessed with an online game. To encourage users to log in, this game will give u ...

  4. ECMAScript 6 入门学习笔记(一)——let和const

    一.let ①声明变量 let a = 1: ②只在所在代码块内有效,不影响块以外 ③不存在变量提升(不能先用后声明) ④暂时性死区 let声明的变量“绑定”这个区域,不受外部影响. let声明之前, ...

  5. Hackerrank manasa-and-combinatorics(数学推导)

    题意:有n个字符A,2n个字符B,问你能用这3n个字母组成多少种字符串,使得组成的字符串所有前缀与后缀的B的数目都大于等于A的数目,对答案mod 99991 分析:类似卡特兰数 ans=总方案数-存在 ...

  6. CH上的Think Bear#1模拟赛

    题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20%20Thinking%20Bear%20%231%20(NOIP%E6%A8 ...

  7. Animations动画和Keyframes关键帧

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. CHAPTER 1 Architectural Overview of Oracle Database 11g

    Which SGA structures are required, and which are optional? The database buffer cache, log buffer, an ...

  9. MySQL用户及数据安全专题

    1 简单介绍 1.1 概要 1.2 权限分类 2 加入用户     2.1 语法例如以下: CREATE USER user_specification [, user_specification] ...

  10. sizeof小览

    一.文章来由-一道面试题迁出的探究 我发现我已经形成一种习惯写来由了,以后看博客的时候能够让我回顾起为什么出现这个问题,我用什么方法解决的,既然形成习惯就让这个习惯保持下去吧.今天实验室师姐在看书,一 ...