【转载请注明】http://www.cnblogs.com/igoslly/p/8670038.html

链表逆序在链表题目中还是较为常见的,这里将Leetcode中的两道题放在一起,分别是

0092 Reverse Linked List  II 0206 Reverse Linked List,0092在0206的基础上,提出了部分逆序的概念


首先来看0206,题目描述为:Reverse a singly linked list.

也就是给出一个链表,要求我们将链表全部倒置链接

想法1: 定义一个堆栈,遍历链表时均push入堆栈,pop的顺序即为逆序

/**
* 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) {
stack<ListNode*> list_stack; // 元素堆栈
ListNode *p = head; // 当链表为空时,直接返回空
if(head==NULL){return NULL;} // 循环结束后,p为链表最后一个有值的结点(非NULL)
while(p->next!=NULL){
list_stack.push(p);
p=p->next;
} // 逆序表表头为原链表末结点,即为p
head=p;
while(!list_stack.empty()){
ListNode *q = list_stack.top();
list_stack.pop();
p->next=q;
p=p->next;
} // 此时p是原链表首元素,新链表末元素,必须赋值NULL,否则提交回超时
p->next=NULL;
return head;
}
};

按照此方法提交后,Leetcode runtime 是 9ms

程序的时间复杂度是 O(2n),因为遍历两次;

空间复杂度是 O(n),stack大小取决于原链表的大小。


想法2: 在遍历原链表时,直接创造新逆序链表,想法大概如下:

/**
* 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) {
ListNode * new_head=NULL;
while(head!=NULL){
ListNode * next=head->next;
head->next=new_head;
new_head=head;
head=next;
}
return new_head;
}
};

这种方法程序的时间复杂度,因为只遍历了一遍,是 O(n),同时也没有耗费多余的空间


再看题目0092,题目描述是

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 ≤ mn ≤ length of list.

要求原链表里[m,n]位置的结点进行逆序

同时保证输入m,n合法

注意: 这里结点计算从1开始

相对与题1来说,题2中相对于逆序段的逆序过程不变,只是多出了逆序段前后结点连接的问题,涉及到的结点有:

①逆序段开始的前一个结点   before_modify     ②逆序段开始的第一个结点 first_modify

③逆序段结束的最后一个结点 last_modify        ④逆序段结束后的后一个结点 after_modify

正常的链表顺序是 ① -> ② -> ③ -> ④

逆序后链表顺序是 ① -> ③ -> ② -> ④

/**
* 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) {
ListNode * pre_head =NULL,*p=head;
// pre_head=before_reverse, p=first_reverse
ListNode * new_head=NULL;
int cnt=; //count linked list position // 使用p和pre_head确定逆序段开始结点和前结点的位置
while(p!=NULL){
cnt++;
if(cnt==m){break;}
pre_head=p;p=p->next;
}
// 循环结束后,pre_head即为前结点位置(1),p为逆序段开始位置(2) // 因为后面循环p位置会改变,所以此时做下记录
// 在逆序后,此结点(2)就作为逆序段末结点,直接连接后续的结点
ListNode * reverse_first=p; // 逆序段开始逆序
while(cnt<=n){
ListNode *Next=p->next;
p->next=new_head;
new_head=p;
p=Next;
cnt++;
}
// 循环结束后,此时p为逆序段结束的后一个结点(4),连接在reverse_first(2)后面
reverse_first->next=p; // 注意:当m=1时,pre_head=NULL,直接返回逆序链表的new_head即可
// 否则,将new_head连接在前面正常后,返回原链表的head
if(pre_head!=NULL){
pre_head->next=new_head;return head;
}else{
return new_head;
}
}
};

Leetcode0092 & 0206--Reverse Linked List 链表逆转的更多相关文章

  1. 【LeetCode每天一题】Reverse Linked List(链表反转)

    Reverse a singly linked list. Example:           Input: 1->2->3->4->5->NULL          ...

  2. [LeetCode]206. Reverse Linked List(链表反转)

    Reverse a singly linked list. click to show more hints. Subscribe to see which companies asked this ...

  3. Leetcode 206 Reverse Linked List 链表

    将单向链表反转 完成如图操作,依次进行即可 1 2 3 /** * Definition for singly-linked list. * struct ListNode { * int val; ...

  4. 【easy】206. Reverse Linked List 链表反转

    链表反转,一发成功~ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; ...

  5. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  6. [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-> ...

  7. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  8. 反转链表 Reverse Linked List

    2018-09-11 22:58:29 一.Reverse Linked List 问题描述: 问题求解: 解法一:Iteratively,不断执行插入操作. public ListNode reve ...

  9. 链表 206 Reverse Linked List, 92,86, 328, 2, 445

    表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...

随机推荐

  1. css3 background-clip和background-origin 区别

    在css3中,background-clip和background-origin它们2个的功能大致相同,但又有些细微差别. 1.background-clip:规定背景的绘制区域,当背景是纯颜色时与图 ...

  2. python运算符及优先级

    计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算. 一.算数运算 以下假设变量:a=10,b=20 二.比较运算 ...

  3. Ac自动机基础题集合

    Ac_automaton的板子打熟以后发现碰到题不会做,而且还是比较纯的板子,只要改几处地方就可以,Ac_automation有许多优秀而fantasy的性质,下面粘几个题,来记录一下做题的心得. 1 ...

  4. 3D全景之ThreeJs

    3D全景之ThreeJs 一.前言 随着H5越来越多的被应用到各个领域,3D也越来越频繁的出现在各个H5案例中,今天我们就来讨论一下3D全景的实现. 据百度百科上介绍:720全景是视角超过人的正常视角 ...

  5. 【[Offer收割]编程练习赛14 D】剑刃风暴(半径为R的圆能够覆盖的平面上最多点数目模板)

    [题目链接]:http://hihocoder.com/problemset/problem/1508 [题意] [题解] 求一个半径为R的圆能够覆盖的平面上的n个点中最多的点数; O(N2log2N ...

  6. Leetcode 42.接雨水

    接雨水 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下 ...

  7. [tsA1491][2013中国国家集训队第二次作业]家族[并查集]

    m方枚举,并查集O(1)维护,傻逼题,,被自己吓死搞成神题了... #include <bits/stdc++.h> using namespace std; struct tri { i ...

  8. [bzoj1634][Usaco2007 Jan]Protecting the Flowers 护花_贪心

    Protecting the Flowers 护花 bzoj-1634 Usaco-2007 Jan 题目大意:n头牛,每头牛有两个参数t和atk.表示弄走这头牛需要2*t秒,这头牛每秒会啃食atk朵 ...

  9. 零基础学python-2.17 文件、open()、file()

    今天我们来说说文件,以及跟文件有关的内建函数open和file 首先我们在python的根文件夹下建一个名为"123"的txt文本文件 文件中面我们输入一些文本 watermark ...

  10. CockroachDB——类似spanner的开源版,底层使用rocksdb存储,mvcc,支持事务,raft一致性,licence是CockroachDB Community License Agreement

    摘自:https://github.com/cockroachdb/cockroach/blob/master/docs/design.md CockroachDB is a distributed ...