单链表 - 单链表的反转 - 腾*和*迅的面试题
发布日期:2021-06-30 16:57:26 浏览次数:2 分类:技术文章

本文共 3849 字,大约阅读时间需要 12 分钟。

文章目录

说明

运行时间O(N)

首先,弄一个简单的单链表,包括

  1. 首尾节点、大小
  2. add()、show()、Node静态内置类

然后,添加reverse(),实现反转功能

最后,写测试方法,@Test调用

其中,reverse的实现思路为:

(转向操作)

  1. 创建 i n d e x N o d e N e x t S h o u l d \color{#4285f4}{indexNodeNextShould} indexNodeNextShould,指向上一个节点的信息
  2. 创建 i n d e x N e x t N o d e \color{#34a853}{indexNextNode} indexNextNode ,指向下一个节点的信息
  3. 当 前 节 点 \color{#ea4335}{当前节点} 连接 i n d e x N o d e N e x t S h o u l d \color{#4285f4}{indexNodeNextShould} indexNodeNextShould

(为下次循环做准备)

  1. i n d e x N o d e N e x t S h o u l d \color{#4285f4}{indexNodeNextShould} indexNodeNextShould指向 当 前 节 点 \color{#ea4335}{当前节点} 作为上一个节点
  2. 当 前 节 点 \color{#ea4335}{当前节点} 指向 i n d e x N e x t N o d e \color{#34a853}{indexNextNode} indexNextNode (当前节点后移)

g o o g l e \color{#4285f4}{g}\color{#ea4335}{o}\color{#fbbc05}{o}\color{#4285f4}{g}\color{#34a853}{l}\color{#ea4335}{e} google

代码实现

简单的单链表

package cn.edut.test;import org.junit.Test;public class MySingleLinkedList
{
private Node
first ; private Node
last ; private int size; public boolean add(T e) {
if(first==null) {
//第一元素添加 first =new Node
(e,null); last = first; }else {
//非第一次 last.next = new Node
(e, null); last = last.next; } size++; return true; } public void show() {
System.out.print("["); Node
next = first; for(int index=0 ; index
{ private Node
next ; private T elementDate ; public Node(T e , Node
nex) { this.elementDate = e ; this.next = nex; } }}

reverse()

public void reverse() {
//没初始化,只有一个值 if(first==null||first==last) {
return ; } //记录当前操作节点 Node
indexNode = first; //存储上一个节点 Node
indexNodeNextShould = null; while(true) {
//记录下一节点 Node
indexNextNode = indexNode.next; //当前节点的next==上一个节点 indexNode.next = indexNodeNextShould ; //存储上一个节点记录往后移 indexNodeNextShould = indexNode; if(indexNode == last) {
break; }else {
//操作节点后移 indexNode = indexNextNode; } } //首尾节点指向调转 Node
temp = last; last = first ; first = temp ; }

@Test调用

/**	 * 测试 创建、add、清除、reverse、Iterator	 */	@Test	public void test01() {
//弄几个数据进去 MySingleLinkedList
mll = new MySingleLinkedList
(); while(mll.size<10) {
mll.add(mll.size); } System.out.print("调转前:");mll.show(); //数据调转 mll.reverse(); System.out.print("调转后:");mll.show(); }

完整代码

package cn.edut.test;import org.junit.Test;public class MySingleLinkedList
{
/** * 测试 创建、add、清除、reverse、Iterator */ @Test public void test01() {
//弄几个数据进去 MySingleLinkedList
mll = new MySingleLinkedList
(); while(mll.size<10) {
mll.add(mll.size); } System.out.print("调转前:");mll.show(); //数据调转 mll.reverse(); System.out.print("调转后:");mll.show(); } private Node
first ; private Node
last ; private int size; public boolean add(T e) {
if(first==null) {
//第一元素添加 first =new Node
(e,null); last = first; }else { //非第一次 last.next = new Node
(e, null); last = last.next; } size++; return true; } public void show() { System.out.print("["); Node
next = first; for(int index=0 ; index
{ private Node
next ; private T elementDate ; public Node(T e , Node
nex) { this.elementDate = e ; this.next = nex; } } public void reverse() { //没初始化,只有一个值 if(first==null||first==last) { return ; } //记录当前操作节点 Node
indexNode = first; //存储上一个节点 Node
indexNodeNextShould = null; while(true) { //记录下一节点 Node
indexNextNode = indexNode.next; //当前节点的next==上一个节点 indexNode.next = indexNodeNextShould ; //存储上一个节点记录往后移 indexNodeNextShould = indexNode; if(indexNode == last) { break; }else { //操作节点后移 indexNode = indexNextNode; } } //首尾节点指向调转 Node
temp = last; last = first ; first = temp ; }}

测试结果

在这里插入图片描述

转载地址:https://lawsssscat.blog.csdn.net/article/details/103002473 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:《数据结构与算法分析-java语言描述》 - 表达式树 -后缀表达式录入成表达式树
下一篇:《数据结构与算法分析-java语言描述》- 练习3.6 “死亡轮盘” Josephus问题

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年04月12日 15时01分57秒