【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) ...
随机推荐
- Django-报错解决方法
无法使用Django新建项目:'django-admin.py’不是内部或外部命令找到site-packages/django/bin(如 D:\Program Files\Anaconda3\Lib ...
- SQL函数小记
写一篇笔记,记录一下常见的sql函数,以备之后的查找需要. 算数函数 abs(num):绝对值函数 mod(被除数,除数):求余函数 round(num,保留小数的位数):四舍五入函数 字符串函数 c ...
- HTTP 请求的 GET 与 POST 方式的区别
HTTP 请求的 GET 与 POST 方式的区别 在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST. GET - 从指定的资源请求数据. POST - 向指定的资源 ...
- git删除远程remote分支
git 命令如下: git push origin --delete <远程分支名字>
- java 日历计算农历和节假日的工具类
背景 业务需求需要后端提供这样的接口,网上找了很多java代码例子,虽然功能实现了 但是不完善,特别是节日那一块儿.然后百度发现有这样的插件,但是信息也是java后端提供的非js 然后在开源js插件找 ...
- zoj 2110 很好的dfs+奇偶剪枝
//我刚开始竟然用bfs做,不断的wa,bfs是用来求最短路的而这道题是求固定时间的 //剪纸奇偶剪枝加dfs #include<stdio.h> #include<queue> ...
- tyvj1117 拯救ice-cream
背景 天好热……Tina顶着那炎炎的烈日,向Ice-cream home走去……可是……停电了……冰淇淋们躺在Ice-cream home的冰柜里,慢慢地……慢慢地……融化…………你说,她能赶在冰 ...
- Hihocoder 1325 (splay)
Problem 平衡树 Treap 题目大意 维护一个数列,支持两种操作. 操作1:添加一个数x. 操作2:询问不超过x的最大的数. 解题分析 尝试了一下用指针来写splay,感觉写起来还是比较流畅的 ...
- JAVA NIO 之Channel
缓冲区本质上是一块可以写入数据,然后可以从中读取数据的内存.Channel 通道就是将数据传输给 ByteBuffer 对象或者从 ByteBuffer 对象获取数据进行传输. Channel 用于在 ...
- Mutual Training for Wannafly Union #6
A =w= B QvQ C 题意:有长度为n的序列(n<=5e5),求满足条件的a,b,c,d的组数,要求满足条件:min([a,b])<=min([c,d]),a<=b<c& ...