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 ...
随机推荐
- [HNOI2002]跳蚤 【容斥】
题目描述 Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持人会给该跳蚤发一张卡片.卡片上写有N+1个自然数.其中最 ...
- Redis的Set无序集合命令
Set是集合,它是string类型的无序集合.set是通过hash table实现的,添加.删除和查找的复杂度都是0(1).对集合我们可以取并集.交集.差集.通过这些操作我们可以实现sns中的好友推荐 ...
- 跨域通信的解决方案JSONP
在web2.0时代,熟练的使用ajax是每个前端攻城师必备的技能.然而由于受到浏览器的限制,ajax不允许跨域通信. JSONP就是就是目前主流的实现跨域通信的解决方案. 虽然在在jquery中,我们 ...
- hive1.1.0建立外部表关联HDFS文件
0. 说明 已经安装好Hadoop和hive环境,hive把元数据存储在mysql数据库.这里仅讨论外部表和HDFS的关联,并且删掉外部表之后,对HDFS上的文件没有影响. 1. 在HDFS创建分区, ...
- ORACLE获取某个时间段之间的月份列表
返回1-31,或者1-12,或者某个 select rownum from dual connect by rownum<31 就是connect by http://marcospri ...
- Codeforces Round #209 (Div. 2)A贪心 B思路 C思路+快速幂
A. Table time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...
- 洛谷P1948 [USACO08JAN]电话线Telephone Lines
题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is u ...
- std::sort运行出core(segment fault)
http://note.youdao.com/noteshare?id=6aae09345e85ab55fe24ac959118a747
- Hadoop YARN 的工作流程简述
1.Client 向 YARN 提交应用程序,其中包括 ApplicationMaster 程序及启动 ApplicationMaster 命令2.ResourceManager 为该 Applica ...
- gitlab之邮箱配置
一.gitlab配置邮件通知功能 编辑/etc/gitlab/gitlab.rb 文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #配置如下 gitlab_rai ...