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 ...
随机推荐
- 2017-2018-2 20165218 实验四《Android开发基础》实验报告
实验三 Android开发基础 课程:java程序设计 姓名:赵冰雨 学号:20165218 指导教师:娄嘉鹏 实验日期:2018.4.14 实验内容: 1.基于Android Studio开发简单的 ...
- MATLAB2010安装方法
MATLAB2010安装方法 第一步选择无网络安装. 选择yes,然后点击next 激活序列号在crack文件夹中的txt文档中 这一步按照图片上的显示操作就可以 选择经典安装 按提示操作,这一步事激 ...
- shell 脚本判断linux 的发行版本
原文vi ./Get_Dist_Name.sh #!/bin/bash Get_Dist_Name() { if grep -Eqii "CentOS" /etc/issue || ...
- No qualifying bean of type [java.lang.String] found for dependency: expected
出现这个问题的原因是因为多写了一个注解但是没有实体类.如: 或者其他注解下没有内容.
- debian自动挂载ntfs硬盘
首先下载安装ntfs-3g apt-get install ntfs-3g 然后查看分区信息 fdisk -l Device Boot Start End Blocks Id System /dev/ ...
- Python学习笔记(八)sorted
摘抄自:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431823058 ...
- 关于 Capella 需要纠正的语音
li { font-size: 18px; } 关于 Capella 需要纠正的语音 持续更新 浊塞音声带要振动 区分 [θ]/[ð] 和 [t̪],注意舌位 [ɫ] 的舌位,切记不能圆唇 [æ] 的 ...
- bootstrap下laydate样式错乱问题
查看发现bs使用了 * {box-sizing:border-box;}重置了盒子模型 那么我们再把它重置回来,在样式中加入以下代码 .laydate_box, .laydate_box * { bo ...
- net 加密-解密
#region DES加密 解密 //key:32位 public string DESEncrypt(string strSource, byte[] key) { System.Security. ...
- 【BZOJ】1036 [ZJOI2008]树的统计Count
[算法]树链剖分+线段树 [题解]模板题,见http://www.cnblogs.com/onioncyc/p/6207462.html 调用线段数时要用新编号pos[i] !!! #include& ...