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. 洛谷 2344 奶牛抗议 Generic Cow Protests, 2011 Feb

    [题解] 我们可以轻松想到朴素的状态转移方程,但直接这样做是n^2的.所以我们考虑采用树状数组优化.写法跟求逆序对很相似,即对前缀和离散化之后开一个权值树状数组,每次f[i]+=query(sum[i ...

  2. Apache Maven Cookbook(八)学习笔记-Handling Typical Build Requirements

    Including and excluding additional resources Using the Maven Help Plugin: mvn help:effective-pom mvn ...

  3. 九度oj 1179 阶乘

    题目1179:阶乘 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6010 解决:1756 题目描述: 输入n,求y1=1!+3!+...m!(m是小于等于n的最大奇数)y2=2!+4!+ ...

  4. ZOJ 1516 Uncle Tom's Inherited Land

    题目大意: 除去那些作为荷塘的土地块,将剩余的土地希望每次将两块相邻的地一起卖出,最多能卖出多少种这样的由相邻土地 合成的长方形土地块 很明显的二分图问题,但是要考虑如何建模 一个长方形土地总是由相邻 ...

  5. 洛谷—— P2658 汽车拉力比赛

    https://www.luogu.org/problem/show?pid=2658 题目描述 博艾市将要举行一场汽车拉力比赛. 赛场凹凸不平,所以被描述为M*N的网格来表示海拔高度(1≤ M,N ...

  6. 时间插件,js格式化,js某月天数,js某月最后一天日期

    //时间格式化 Date.prototype.format = function(fmt) { var o = { "M+": this.getMonth() + 1, //月份 ...

  7. 【CV论文阅读】:Rich feature hierarchies for accurate object detection and semantic segmentation

    R-CNN总结 不总结就没有积累 R-CNN的全称是 Regions with CNN features.它的主要基础是经典的AlexNet,使用AlexNet来提取每个region特征,而不再是传统 ...

  8. IOS--JSON数据解析成字典

    JSON解析成字典 {} –>字典 [] –>数组 ""–>字符串 11/11.1–>NSNumber true/false –>NSNumber n ...

  9. 大数据分析:结合 Hadoop或 Elastic MapReduce使用 Hunk

    作者 Jonathan Allen ,译者 张晓鹏 Hunk是Splunk公司一款比較新的产品,用来对Hadoop和其他NoSQL数据存储进行探測和可视化,它的新版本号将会支持亚马逊的Elastic ...

  10. CodeForces 16B Burglar and Matches(贪心)

    B. Burglar and Matches time limit per test 0.5 second memory limit per test 64 megabytes input stand ...