【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) ...
随机推荐
- led1,1s取反,led2计数10次取反
1 //利用定时器0 1s,led1取反,利用计数器1,跳10,取反 #include<reg52.h> #define uchar unsigned char #define uint ...
- 腾讯云:基于 Ubuntu 搭建 VNC 远程桌面服务
基于 Ubuntu 搭建 VNC 远程桌面服务 前言 任务时间:5min ~ 10min 必要知识 本教程假设您已学习以下 Ubuntu 基本操作: 连接 SSH 执行命令 编辑文件 如果还没有掌握 ...
- elasticsearch数据库使用
elasticsearch的一个最为显著的优点:快速全文检索.关于elasticsearch 全文检索的原理,请看:https://blog.csdn.net/wolfcode_cn/article/ ...
- 对百词斩&可可英语的测试
第六周小组作业 基本任务:功能测试和测试管理 温馨提示:本篇博客中,看不清的图片,可以按住Ctrl同时滚动鼠标滚轮查看:也可以点击图片下方的链接,在新选项卡打开后,点击小加号查看. (1)计划说明 本 ...
- Leetcode 126.单词接龙II
单词接龙II 给定两个单词(beginWord 和 endWord)和一个字典 wordList,找出所有从 beginWord 到 endWord 的最短转换序列.转换需遵循如下规则: 每次转换只能 ...
- vue.js 利用组件之间通讯,写一个弹出框例子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 数据库删除数据表重复数据,只留下ID较小的行
删除表中重复数据,留下ID比较小的行 delete from 表 where [重复字段] in (select [重复字段] from 表 group by 字段 having count([字段] ...
- hdu_1205_吃糖果_201404021440
吃糖果 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Submis ...
- PayPal加密证书.pem的生成
How do I create a public certificate for use with PayPal Encrypted Website Payments? Before you ca ...
- Unity uGui RawImage 渲染小地图
制作类似 RPG 游戏时,可能会须要显示小地图. 小地图的制作一种方式是用还有一个摄像机来渲染到一张纹理上.实时显示到UI界面. 以Unity 5.0 的 UI 系统为例: 在地图正上方放置一个摄像机 ...