【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List
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的更多相关文章
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,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 ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 【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-> ...
- 【LeetCode】1171. Remove Zero Sum Consecutive Nodes from Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 preSum + 字典 日期 题目地址:https:/ ...
- 【LeetCode】445. Add Two Numbers II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和再构成列表 使用栈保存节点数字 类似题目 日期 ...
- 【leetcode】Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
随机推荐
- FJoi2017 1月20日模拟赛 直线斯坦纳树(暴力+最小生成树+骗分+人工构造+随机乱搞)
[题目描述] 给定二维平面上n个整点,求该图的一个直线斯坦纳树,使得树的边长度总和尽量小. 直线斯坦纳树:使所有给定的点连通的树,所有边必须平行于坐标轴,允许在给定点外增加额外的中间节点. 如下图所示 ...
- Spring常用注解总结 hibernate注解
1.@Resource和@Autowired @Resource和@Autowired功能一样在容器查找匹配的Bean @Autowired默认按照byType方式进行bean匹配,@Resource ...
- 九度oj 题目1053:互换最大最小数
题目1053:互换最大最小数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7538 解决:3049 题目描述: 输入一个数n,然后输入n个数值各不相同,调换数组中最大和最小的两个数,然后 ...
- noip模拟赛 蒜头君救人
分析:之前的一道模拟赛题是dp+dfs,这道题是dp+bfs. 我们设f[stu][i][j]为当前状态为stu,走到(i,j)的答案,考虑怎么设计stu,每个人的状态有3种:要么在原地,要么被背着, ...
- [codevs 1243][网络提速(最短路分层思想)
题目:http://dev.codevs.cn/problem/1243/ 分析: 先容易想到将一个点拆成m个点,分别对应不同的边连过去,但是想不到控制加速器数量的办法.看了题解才知道,每个点的分层, ...
- Redis3.0集群方案分析
在Redis3.0集群出来之前,大家都对作者antirez寄予厚望,因为Redis从来没有让我们失望过.现在Redis3.0集群出来了,网上出了很多评论文章,都说他的功能多么强大,包括下面这张图是彻底 ...
- Linux Rsync实现文件同步备份(转载)
原文地址:Linux Rsync实现文件同步备份作者:夷北 转自:http://www.mike.org.cn/blog/index.php?load=read&id=639###pp=0 [ ...
- Ajax异步方式实现登录与參数的校验
登录代码 这个是使用Bootstrap3的组件功能实现的 <div class="login_con_R"> <h4>登录</h4> <F ...
- ARP病毒查找与防范
笔者是一名大学生,学的是网络project. 自然接触的网络就多了.当然免不了做些坏事,嘿嘿. 比如我经经常使用在网上学习的知识去測验一下我自己的电脑或者同学啥的,都是一些小打小闹. 无伤大雅,知道有 ...
- C# 文件里的类不能进行设计,因此未能为该文件显示设计器
C# 文件里的类不能进行设计,因此未能为该文件显示设计器 vs 一直打不开设计界面 仅仅能查看代码界面 这时候须要查看 代码中 是不是 从 form 继承 假设不是 窗口类型 改为 fo ...