Java--算法--链表
- 单链表的介绍:
- 单链表的实际应用
-
package com.model.linkedlist.single; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/6 20:50 */ public class SingleLinkedListDemo01 { public static void main(String[] args) { Node node1 = new Node(1, "张三"); Node node2 = new Node(2, "李四"); Node node3 = new Node(3, "王五"); Node node4 = new Node(4, "马六"); SingleLinkedList list = new SingleLinkedList(); list.add(node1); list.add(node2); list.add(node4); list.add(node3); list.show(); System.out.println("----------------------------"); SingleLinkedList list2 = new SingleLinkedList(); list2.addById(node3); list2.addById(node1); list2.addById(node4); list2.addById(node2); //修改节点信息 node2.name="aaa"; list2.update(node2); list2.delete(node1); list2.delete(node2); list2.delete(node3); list2.delete(node4); list2.show(); } } class SingleLinkedList{ public Node head=new Node(); //添加节点 public void add(Node node){ Node temp=head; while (true){ if (temp.next==null){ break; } temp=temp.next; } temp.next=node; } //按顺序添加节点 public void addById(Node node){ Node temp=head; boolean flag=true; while (true){ if (temp.next==null){ break; }if(temp.next.id>node.id){ break; } if (temp.next.id==node.id){ flag=false; break; } temp=temp.next; } if (flag){ Node temp2=temp.next; temp.next=node; node.next=temp2; }else { System.out.println("已经添加这个节点了,不能重重复添加"); } } // 修改节点信息 public void update(Node node){ if(isEmpty()){ System.out.println("链表为空,不能修改"); return; } boolean flag=false; Node temp=head.next; while(true){ if (temp==null){ break; } if (temp.id==node.id){ flag=true; break; } temp=temp.next; } if (flag){ temp.name=node.name; }else { System.out.println("没有找到相关的节点"); } } //删除节点 public void delete(Node node){ if (isEmpty()){ System.out.println("链表为空,不能删除"); return; } boolean flag=false; Node temp=head; while (true){ if (temp.next==null){ break; } if (temp.next.id==node.id){ flag=true; break; } temp=temp.next; } if (flag){ temp.next=temp.next.next; }else { System.out.println("没有找到相关的节点"); } } //判断节点是否为空 public boolean isEmpty(){ if (head.next==null){ return true; } return false; } //遍历节点 public void show(){ //1.判断是否为空 if (isEmpty()){ System.out.println("链表为空"); return; } Node temp= head.next; while (true){ if (temp==null){ break; } System.out.println(temp.toString()); temp=temp.next; } } } class Node{ public int id; public String name; public Node next; public Node() { } public Node(int id, String name, Node next) { this.id = id; this.name = name; this.next = next; } public Node(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "Node{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
- 单链表的面试题:
-
-
package com.model.linkedlist.single; import javax.xml.crypto.dsig.SignatureMethod; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/7 22:06 * 演示单链表练习题 */ public class SingleLinkedListDemo03 { public static void main(String[] args) { SingleLinkedList03 linkedList03 = new SingleLinkedList03(); HeroNode node1 = new HeroNode(1, "张三"); HeroNode node2 = new HeroNode(2, "李四"); HeroNode node3 = new HeroNode(3, "王五"); HeroNode node4 = new HeroNode(4, "马六"); linkedList03.add(node1); linkedList03.add(node2); linkedList03.add(node3); linkedList03.add(node4); linkedList03.show(); System.out.println("----------------------------"); SingleLinkedList03 list03 = new SingleLinkedList03(); list03.addInOrder(node4); list03.addInOrder(node3); list03.addInOrder(node2); list03.addInOrder(node1); list03.show(); System.out.println("修改之后的链表:"); node1.name="张紫韩"; list03.update(node1); list03.show(); System.out.println("删除之后的链表"); list03.delete(node2); list03.show(); System.out.println("---------------------------------"); //统计一个链表的个数链表的个数 SingleLinkedList03 list = new SingleLinkedList03(); list.addInOrder(node1); list.addInOrder(node2); list.addInOrder(node3); list.addInOrder(node4); System.out.println("节点数:"+nodeCount(list)); System.out.println("倒数第kge节点"+getHeroNode(list, 2)); } // 1. 查找单链表的个数 public static int nodeCount(SingleLinkedList03 list){ if (list.isEmpty()){ return 0; }else { HeroNode temp = list.getHead().next; int count = 0; while(temp!=null){ count++; temp = temp.next; } return count; } } // 2. 查找单链表中倒数第k个节点 public static HeroNode getHeroNode(SingleLinkedList03 list,int k){ if (list.isEmpty()){ return null; } else { HeroNode temp=list.getHead().next; int count = nodeCount(list); if (k>count||k<=0){ return null; }else { int n=count-k+1; for (int i = 1; i!=n ; i++) { temp=temp.next; } return temp; } } } } class SingleLinkedList03{ private HeroNode head=new HeroNode();//头节点 public HeroNode getHead(){ return head; } //判断是否为空 public boolean isEmpty(){ if (head.next==null){ return true; } return false; } //加在链表的最后 public void add(HeroNode node){ HeroNode temp=head; while(true){ if (temp.next==null){ temp.next=node; break; } temp=temp.next; } } //按顺序添加 public void addInOrder(HeroNode node){ if (isEmpty()){ System.out.println(""); } HeroNode temp=head; boolean flag=false; while(true){ if(temp.next==null){ flag=true; break; } if (temp.next.id>node.id){ flag=true; break; } if (temp.next.id==node.id){ break; } temp=temp.next; } if (flag){ HeroNode temp2=temp.next; temp.next=node; node.next=temp2; }else { System.out.println("已经添加过此元素不能重复添加"); } } // 修改节点 public void update(HeroNode node){ if (isEmpty()){ System.out.println("链表为空,不能进行修改"); return; } HeroNode temp=head.next; boolean flag=false; while(true){ if (temp==null){ break; } if (temp.id==node.id){ flag=true; break; } temp=temp.next; } if (flag){ temp.name= node.name; }else { System.out.println("链表中没有找到此节点,无法进行修改"); } } // 删除节点 public void delete(HeroNode node){ if (isEmpty()){ System.out.println("链表为空,无法进行删除"); return; } HeroNode temp=head; boolean flag=false; while(true){ if (temp.next==null){ break; } if (temp.next.id==node.id){ flag=true; break; } temp=temp.next; } if (flag){ temp.next=temp.next.next; }else { System.out.println("此链表上没有相关节点,无法进行删除"); } } // 遍历链表 public void show(){ if (isEmpty()){ System.out.println("链表为空,没有节点"); return; } HeroNode temp=head.next; while (true){ if (temp==null){ break; }else { System.out.println(temp); temp=temp.next; } } } } class HeroNode{ public int id; public String name; public HeroNode next; public HeroNode() {} public HeroNode(int id, String name, HeroNode next) { this.id = id; this.name = name; this.next = next; } public HeroNode(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return "HeroNode{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
-
-
// 将单链表进行反转
public static void reverse(SingleLinkedList03 list) {
if (list.isEmpty() || list.getHead().next.next == null) {
return;
}
HeroNode cur = list.getHead().next;
HeroNode reverseHead = new HeroNode();
HeroNode next=null;
while (true) {
if (cur==null){
break;
}else {
next=cur.next; //类类型的数据传递的是地址,改变原来的值会改变原来的值
//因为要取出cur节点,所以必须要保存一个next节点,要不然单链表会断掉
cur.next = reverseHead.next;
reverseHead.next=cur;
}
cur=next;
}
list.getHead().next = reverseHead.next;
}
//逆序打印单链表 public static void print(SingleLinkedList03 list){ if (list.isEmpty()){ System.out.println("链表为空"); return; } HeroNode temp = list.getHead().next; Stack<HeroNode> stack = new Stack<>(); while(true){ if (temp==null){ break; } stack.add(temp); temp=temp.next; } while (stack.size()>0){ System.out.println(stack.pop()); } } //合并连个有序的链表 public static SingleLinkedList03 merge(SingleLinkedList03 list1,SingleLinkedList03 list2){ SingleLinkedList03 res = new SingleLinkedList03(); if (list1.getHead().next!=null) { HeroNode temp=list1.getHead().next; HeroNode next=null; while (true) { if (temp == null) { break; }else { next=temp.next; res.addInOrder(temp); } temp=next; } } if (list2.getHead().next!=null) { HeroNode temp2=list2.getHead().next; HeroNode next=null; while (true) { if (temp2 == null) { break; }else { next=temp2.next; res.addInOrder(temp2); } temp2=next; } } return res; }
-