ReverseLinkedList(翻转链表)

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。非连续、非顺序指的是,通过指针把一组零散的内存块串联在一起,其中每一个内存块叫做链表的节点,所以每个节点包含两部分一个data(你存放的数据),一个next(指向下一个节点)当然这里使用单向链表举例。双向链表则有两个指向节点一个next(下一个节点)一个prev(上一个节点),对比:双向链表虽然更麻烦,但是比单向链表更受欢迎,因为,他记录了上一个节点,节省了操作数据时候的时间,但是需要用内存换时间。

 单向链表:

双向链表;

题目一:

给定一组数据,从尾到头进行翻转

Input:  1 ->2->3->4->5

Output: 5->5->3->2->1

思路:使用是三个变量进行控制(prev、current、next),模拟一个窗口,进行对数据的推进,进而翻转,不断改变三个元素的位置。

/**
* @author : lizi
* @date : 2021-03-02 17:05
**/
public class ListNode {
int val = 0;
ListNode next; ListNode(int x) {
val = x;
} public void setVal(int val) {
this.val = val;
} public void setNext(ListNode next) {
this.next = next;
}
}
    private static ListNode reverseList(ListNode head) {
if (head == null) return null;
ListNode prev=head;
ListNode current=head.next;
// because the last Node point into null,so let fist node.next point into null
prev.next=null;
while (current!=null){
ListNode next=current.next;
// that is place where change node. change position of current and prev
current.next=prev;
prev=current;
// change current into next (in the way,the window is pushed)
current=next;
}
return prev;
}
}

题目二:

obviously,we should change position of m and n 

Input:1->2->3->4->5->null ,m=2,n=5

Output:1->4>3>2>5>null 

tip: actually, many companies like to make this question as ultimate question,also the question was Amazon’s question for  interviewee,however, we remain can use method in reversing whole linkedList. i mean that method of pushing window.

    private static ListNode reverseBetween(ListNode head, int m, int n) {
// if you can not find head, you can make the node.next to find head, todo actually, it is a node that prevent you can not find first node
ListNode dummy = new ListNode(-1);
dummy.next=head;
head=dummy;
// to find a place where you began to control list
for (int i = 1; i <m ; i++) {
head=head.next;
}
ListNode prevM=head;
ListNode mNode=prevM.next;
ListNode nNode=mNode;
ListNode postN=nNode.next;
//todo visibly, the portion is method of window that i mention in method of reversing the whole window
for (int i = m; i <n ; i++) {
ListNode next=postN.next;
postN.next=nNode;
nNode=postN;
postN=next;
}
prevM.next=nNode;
mNode.next=postN;
// you can get dummy node.next in this way ,find the first node
return dummy.next;
}

finally: to do examine

    public static void main(String[] args) {
ListNode listNode1 = generateDate();
System.out.println(listNode1.toString());
// reverse whole list
ListNode reverseBetween = reverseList(generateDate());
// reverse m and n in list
ListNode listNode = reverseBetween(listNode1, 2, 4);
printForSomething(reverseBetween);
} // just to print result
public static void printForSomething(ListNode listNode) {
if (listNode != null) {
System.out.println(listNode.val);
printForSomething(listNode.next);
}
}

the picture is what i visualize method how to run:

To summarize:

as a beginner,personally, it is abstract to imagine how the cell of linkedlist  connect with each other,but it is a effective to draw what you imagine. i still want everyone to criticize what i wrong in every article.

reverseLinkedList(翻转链表)的更多相关文章

  1. [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  2. C语言递归,非递归实现翻转链表

    翻转链表作为,链表的常用操作,也是面试常遇到的. 分析非递归分析: 非递归用的小技巧比较多,很容易出错. 递归分析比较简单,在代码里面 代码: #include<stdio.h> #inc ...

  3. [LintCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  4. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

  5. lintcode: 翻转链表

    题目: 翻转链表 翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null 挑战 在原地一次翻转完成 解题: 递归还 ...

  6. 025k个一组翻转链表

    #include "000库函数.h" struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), n ...

  7. 【LeetCode题解】25_k个一组翻转链表(Reverse-Nodes-in-k-Group)

    目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记 ...

  8. LeetCode(15): 每k个一组翻转链表

    hard! 题目描述: 给出一个链表,每 k 个节点为一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. ...

  9. Leetcode题库——25.k个一组翻转链表

    @author: ZZQ @software: PyCharm @file: ReverseList.py @time: 2018/11/6 15:13 题目要求:给出一个链表,每 k 个节点一组进行 ...

随机推荐

  1. mybatis(八)手写简易版mybatis

    一.画出流程图 二.设计核心类 二.V1.0 的实现 创建一个全新的 maven 工程,命名为 mebatis,引入 mysql 的依赖. <dependency> <groupId ...

  2. Commons Collections2分析

    0x01.POC分析 //创建一个CtClass对象的容器 ClassPool classPool=ClassPool.getDefault(); //添加AbstractTranslet的搜索路径 ...

  3. convert URL Query String to Object All In One

    convert URL Query String to Object All In One URL / query string / paramas query string to object le ...

  4. Azure 计费 & 成本管理 & 取消 Azure 订阅

    Azure 计费 & 成本管理 & 取消 Azure 订阅 https://docs.microsoft.com/zh-cn/azure/cost-management-billing ...

  5. c++ 设置桌面壁纸(win)

    #include <iostream> #include <Windows.h> int main() { const char* path = "C:\\Users ...

  6. 未来,Baccarat将如何拓展生态版图?

    Baccarat最近几度冲上DeFi版面的热搜,一是因为BGV价格不断的上涨,二是因为生态建设者的不断涌入.可以说,当下的Baccarat,实在是太火爆了.那么在未来,Baccarat还将会持续火爆吗 ...

  7. PAUL ADAMS ARCHITECT:澳洲房贷最低利率来袭

    11月3日澳洲储备银行宣布将官方现金利率从0.25%降至0.1%,破历史最低纪录.此次澳洲储备银行降息的目的主要是为了刺激经济走出全球经济危机引发的衰退.据了解,这已经是澳洲今年第三次降息,也是自20 ...

  8. Fast R-CNN训练自己的数据集时遇到的报错及解决方案

    最近使用Fast R-CNN训练了实验室的数据集,期间遇到一些报错,主要还是在配置环境上比较麻烦,但可以根据提示在网上找到解决这些错误的办法.这里我只记录一些难改的报错,以后再遇见这些时希望能尽快解决 ...

  9. python 相对路径和绝对路径的区别

    一,Python中获得当前目录和上级目录 获取当前文件的路径: from os import path d = path.dirname(__file__) #返回当前文件所在的目录 # __file ...

  10. MySQL 修改数据表

    修改数据表: 创建数据表 更改表明 更改字段数据类型 更改字段名称 更改字段名称和数据类型 为表添加新字段 将字段顺序改为第一位 将字段顺序改为另一个字段之后 删除字段 1 use test; 2 3 ...