关于链表的细枝末节 (含面试题)2021-06-30
2021/6/30 23:26:20
本文主要是介绍关于链表的细枝末节 (含面试题)2021-06-30,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.h.linkedlist; /** * @Auther: Hao * @Date:2021/6/27 */ public class SingleLinkedListDemo { public static void main(String[] args) { SingleLinkedList singleLinkedList = new SingleLinkedList(); Node node1 = new Node(1, "孙悟空"); Node node2 = new Node(2, "猪八戒"); Node node3 = new Node(3, "沙僧"); Node node4 = new Node(4, "唐僧"); singleLinkedList.addNode(node4); singleLinkedList.addNode(node2); singleLinkedList.addNode(node1); singleLinkedList.addNode(node3); singleLinkedList.qurryLinkedList(); singleLinkedList.flip(); System.out.println("反转之后~~"); singleLinkedList.qurryLinkedList(); } } /** * 链表 */ class SingleLinkedList { //定义一个头节点 Node headNode = new Node(0, "", null); public void addNode(Node node) { Node temp = headNode; while (temp.nextNode != null) { temp = temp.nextNode; } temp.nextNode = node; } public void del(int no) { Node temp = headNode; while (true) { if (temp.nextNode == null) { System.out.println("未找到您要删除的节点,删除失败!"); break; } if (temp.nextNode.no == no) { temp.nextNode = temp.nextNode.nextNode; System.out.println("删除成功!"); break; } temp = temp.nextNode; } } public void update(Node node) { Node temp = headNode.nextNode; while (true) { if (temp == null) { System.out.println("未找到您要更新的节点!更新失败"); break; } if (temp.no == node.no) { temp.name = node.name; System.out.println("更新成功!"); break; } temp = temp.nextNode; } } public void qurryLinkedList() { if (headNode.nextNode == null) { System.out.println("链表为空!"); return; } Node temp2 = headNode.nextNode; while (temp2 != null) { System.out.println(temp2); temp2 = temp2.nextNode; } } /*以下为数据结结构常考面试题*/ //返回链表中有效节点个数 public int nodeNum() { int i = 0; Node temp = headNode.nextNode; while (temp != null) { i++; temp = temp.nextNode; } return i; } //返回链表中倒数第 K 个节点 public Node lastK(int k) { int size = nodeNum(); int times = size - k; Node temp = headNode.nextNode; if (times < 0) { System.out.print("参数错误 "); return null; } for (int i = 0; i < times; i++) { temp = temp.nextNode; } return temp; } //单链表翻转 // 整体思路为:新建一个链表用于暂存数据 遍历链表 每遍历一个结点 放到新链表最前端。 遍历完毕后 将链表头结点的nextNode 指向新链表头结点的nextNode public void flip() { //定义一个新的头结点存放链表 Node newHeadNode = new Node(0, ""); Node cur = headNode.nextNode; Node temp = null; while (true) { //这个地方有一点难懂 需要动脑筋考虑 temp = cur.nextNode; //temp指向cur的下一节点的地址 cur.nextNode = newHeadNode.nextNode; //这里改变的是car指向地址存储的内容 newHeadNode.nextNode = cur; cur = temp; if (cur == null) { break; } } headNode.nextNode = newHeadNode.nextNode; } } /** * 节点 */ class Node { //排名 public int no; //名字 public String name; //下一个节点的信息 public Node nextNode; public Node(int no, String name) { this.no = no; this.name = name; } public Node(int no, String name, Node nextNode) { this.no = no; this.name = name; this.nextNode = nextNode; } @Override public String toString() { return "Node{" + "no=" + no + ", name='" + name + '\'' + '}'; } }
这篇关于关于链表的细枝末节 (含面试题)2021-06-30的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)