Reverse Linked List I&&II——数据结构课上的一道题(经典必做题)
Reverse Linked List I
Question Solution
Reverse a singly linked list.
Reverse Linked List I
设置三个指针即可,非常简单:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL || head->next == NULL){
return head;
}
ListNode* firstNode = head;
ListNode* preCurNode = head;
ListNode* curNode = head->next;//maybe null
while(curNode){
preCurNode->next = curNode->next;
curNode->next = firstNode;
firstNode=curNode;
curNode =preCurNode->next; }
return firstNode;
}
};
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->2->3->4->5->NULL
, m = 2 and n = 4,
return 1->4->3->2->5->NULL
.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if(head==NULL||head->next==NULL||m==n)
return head;
ListNode* firstNode = head;
ListNode* preCurNode = head;
ListNode* curNode = head->next;//maybe null
ListNode* lastNode= head;
int flag=;
int m_flag=flag;
if(m==)
{
while(flag<n)
{
preCurNode->next = curNode->next;
curNode->next = firstNode;
firstNode=curNode;
curNode =preCurNode->next;
flag++;
}
return firstNode;
}
else
{
while(flag<n)
{
if(flag<m)
{
lastNode=firstNode;
firstNode=firstNode->next;
preCurNode =preCurNode->next;
curNode =curNode->next;
flag++;
}
else
{
preCurNode->next = curNode->next;
curNode->next = firstNode;
firstNode=curNode;
curNode =preCurNode->next;
lastNode->next=firstNode;
flag++;
} }
return head;
}
}
};
Reverse Linked List I&&II——数据结构课上的一道题(经典必做题)的更多相关文章
- Majority Element——算法课上的一道题(经典)
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 数据结构必做题参考:实验一T1-20,实验2 T1
实验一T1-10 #include <bits/stdc++.h> using namespace std; ; struct Book { string isbn; string nam ...
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- 【LeetCode练习题】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
1. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no ...
- LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II
1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...
- [LeetCode] 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】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 14. Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
随机推荐
- mysql互为主从复制配置笔记--未读,稍后学习
MySQL-master1:192.168.72.128 MySQL-master2:192.168.72.129 OS版本:CentOS 5.4MySQL版本:5.5.9(主从复制的master和s ...
- 【2016北京集训】crash的游戏
Portal --> broken qwq Description 有个口袋,一开始里面有\(N\)个球,接下来进行\(M\)次操作,每次可以选择往里面放一个球或者从里面拿一个球出来,在这\(M ...
- Lipshitz
Portal --> broken qwq Description 大M正在学习函数的光滑性并对Lipschitz常数非常感兴趣:当一个定义域为\([l,r]\)的函数\(f\),对于定义域内的 ...
- SFM学习
摘自李翠http://www.cnblogs.com/serser/p/6598621.html SFM 1.相机模型,内参数和外参数矩阵,相机标定: 2.极线约束和本征矩阵:特征点提取与匹配:提取到 ...
- git 撤销上一次 commit
1.本地 commit,没有推到远程仓库 可以 git reset --soft <commit_id>,commit_id 是要回退到的某一版本 然后再进行修改,再commit, 如果需 ...
- JAX-WS搭建WebService和客户端访问程序
开发环境:myeclipse8.6+jdk1.6.0_29+tomcat6.0.37 XFire搭建webservice: http://www.cnblogs.com/gavinYang/p/352 ...
- string的内存本质
虽然没有研究过string的源代码,不过可以确定的是string的内存空间是在堆上开辟的,它自己负责释放空间,不用我们关系. 我们用一个动态分配的字符串指针初始化一个string对象retStr,它会 ...
- Maven手动添加依赖的jar文件到本地Maven仓库
原文出处:https://www.iteblog.com/archives/646.html 寫得很好,轉走僅供學習,望諒解! Apache Maven,是一个软件(特别是Java软件)项目管理及自动 ...
- HDU 6061 推导 NTT
复函数,递归代入,可以得到最终的式子为$f(x-\sum_{i=1}^{m}{a_i})$,且$f(x) = \sum_{i = 0}^{n}{c_ix^i}$,求最终各个x项的系数. 设$S=\su ...
- Delphi 常用数据类型 字符串类型 数据类型等等
字符串类型 Delphi有三种类型的字符: ●AnsiChar这是标准的1字节的ANSI字符,程序员都对它比较熟悉. ●WideChar这是2字节的Unicode字符. ●Char在目前Delphi早 ...