在上一篇文章中我有写到怎么将节点有序的添加到单链表中
https://blog.csdn.net/weixin_44076260/article/details/105315461
思路分析:
·创建一个新链表,将原始的表节点连到新链表
·遍历两个链表,通过比较大小,将原始链表节点连到新链表上
·代码实现
package com.alibaba.linkedlist;
import java.util.Stack;
public class SingleLinkedListDemo {
public static void main(String[] args) {
//创建节点
HeroNode heroNode=new HeroNode(1,"宋江","及时雨");
HeroNode heroNode1=new HeroNode(3,"吴用","智多星");
HeroNode heroNode2=new HeroNode(2,"卢俊义","玉麒麟");
HeroNode heroNode3=new HeroNode(7,"林冲","豹子头");
HeroNode heroNode4=new HeroNode(5,"宋江","及时雨");
HeroNode heroNode5=new HeroNode(9,"吴用","智多星");
HeroNode heroNode6=new HeroNode(8,"卢俊义","玉麒麟");
HeroNode heroNode7=new HeroNode(6,"林冲","豹子头");
//创建链表
SingleLinkedList singleLinkedList=new SingleLinkedList();
SingleLinkedList singleLinkedList1=new SingleLinkedList();
//将节点加入到链表中,排序
singleLinkedList.addByOrder(heroNode);
singleLinkedList.addByOrder(heroNode1);
singleLinkedList.addByOrder(heroNode2);
singleLinkedList.addByOrder(heroNode3);
singleLinkedList1.addByOrder(heroNode4);
singleLinkedList1.addByOrder(heroNode5);
singleLinkedList1.addByOrder(heroNode6);
singleLinkedList1.addByOrder(heroNode7);
singleLinkedList.addOrderTwo(singleLinkedList.getHead(), singleLinkedList1.getHead());
}
}
//定义SingleLinkedList管理我们的英雄
class SingleLinkedList{
//先初始化头节点,头节点不要动
private HeroNode head=new HeroNode(0,"","");
public HeroNode getHead() {
return head;
}
public void addOrderTwo(HeroNode heroNode,HeroNode heroNode2){
HeroNode temp=heroNode.next;
HeroNode temp1=heroNode2.next;
HeroNode hero=new HeroNode(0,"","");//创建新的头节点
HeroNode next=hero;//指向下一个节点
while (null!=temp&&null!=temp1){
if(temp.no<=temp1.no){
next.next=temp;
next=temp;
temp=temp.next;
}else{
next.next=temp1;
next=temp1;
temp1=temp1.next;
}
}
//将原始表剩余的节点放到新链表上
while (null!=temp){
next.next=temp;
next=temp;
temp=temp.next;
}
while (null!=temp1){
next.next=temp1;
next=temp1;
temp1=temp1.next;
}
//将链表头指向新的链表的下一个
head.next=hero.next;
list();
}
//逆序打印单链表
public void reversePrint(HeroNode heroNode){
if(heroNode.next==null){
return;
}
//创建一个栈,将各个节点压入栈中
Stack<HeroNode> stack=new Stack<>();
HeroNode temp=heroNode.next;
while (temp!=null){
stack.push(temp);
temp=temp.next;
}
while (stack.size()>0){
System.out.println(stack.pop());//取出栈中的数据
}
}
//将单链表反转
public void reversetList(HeroNode heroNode){
//链表为空或者链表只有一个节点时不需要反转
if(heroNode.next==null||heroNode.next.next==null){
return;
}
HeroNode temp=heroNode.next;
HeroNode next=null;//指向下一个节点;
HeroNode reverseHead=new HeroNode(0,"","");
//遍历链表将最前端的节点取出放入reverseHead中
while (temp!=null){
next=temp.next;
temp.next=reverseHead.next;
reverseHead.next=temp;
temp=next;
}
//将链表头指向新的链表的下一个
heroNode.next=reverseHead.next;
}
//获取单链表倒数第k个节点
public HeroNode getHeroNode(HeroNode heroNode,int k){
if(head.next==null){
System.out.println("链表为null");
return null;
}
int size=getLength(heroNode);
//验证k
if(k<=0||k>size){
return null;
}
HeroNode temp=head.next;
for (int i = 0; i <size -k; i++) {
temp=temp.next;
}
return temp;
}
//获取单链表的长度
public int getLength(HeroNode heroNode){
//判断是否为空
if(head.next==null){
System.out.println("链表为空");
return 0;
}
int length=0;
HeroNode temp=head.next;
while (temp!=null){
temp=temp.next;
length++;
}
return length;
}
//添加节点到单链表
public void add(HeroNode heroNode){
//因为头节点不能动所以需要一个辅助变量 temp
HeroNode temp=head;
//遍历链表,找到最后
while (true) {
//判断temp.next是否是最后
if(temp.next==null){
break;
}
//如果没有找到最后,就将temp后移
temp=temp.next;
}
//当退出循环时,temp指向链表的最后,将最后的节点指向新节点
temp.next=heroNode;
}
public void addByOrder(HeroNode heroNode){
//辅助变量
//因为是单链表,所以要找到添加位置的前一个位置,否则插入不了
HeroNode temp=head;
boolean flag=false;//标志添加的节点是否存在,默认为false
while (true){
if(temp.next==null){//说明已经在链表的最后了
break;
}
if(temp.next.no>heroNode.no){//位置找到,就在temp的后面
break;
}else if(temp.next.no==heroNode.no){//说明改变已存在
flag=true;
break;
}
//未找到后移继续找
temp=temp.next;
}
//判断flag的值
if(flag){//不能添加
System.out.printf("此编号%d已存在,不能重复添加\n",heroNode.no);
}else{
//插入到链表中,temp后面
heroNode.next=temp.next;
temp.next=heroNode;
}
//循环结束,已找到位置
temp.next=heroNode;
}
//修改节点信息,根据no来修改
public void update(HeroNode heroNode){
//判断是否为空
if(head.next==null){
System.out.println("链表为空");
return;
}
//找到需要修改的节点
HeroNode temp=head.next;
boolean flag=false;//表示节点是否被找到
while (true){
if(temp==null){
break;//表示链表已经遍历结束
}
if(temp.no==heroNode.no){
//找到要修改的节点
flag=true;
break;
}
temp=temp.next;
}
//根据flag是否找到要修改的节点
if(flag){
temp.name=heroNode.name;
temp.nickname=heroNode.nickname;
}else{
System.out.printf("没有找到要修改的节点%d\t",heroNode.no);
}
}
//删除节点,需要找到要删除节点的前一个节点
public void del(int no){
HeroNode temp=head;
boolean flag=false;//是否找到待删除节点
while (true){
if(temp.next==null){
System.out.println("已经到链表的最后了");
break;
}
if(temp.next.no==no){//找到待删除节点的前一个
flag=true;
break;
}
temp=temp.next;
}
if(flag){
temp.next=temp.next.next;
}else{
System.out.printf("要删除的节点%d不存在",temp.no);
}
}
//遍历链表显示
public void list(){
//判断链表为空
if(head.next==null){
System.out.println("链表为空");
return;
}
//因为头节点不能动所以需要一个辅助变量
HeroNode temp=head.next;
while (true){
//判断链表是否到最后
if(temp==null){
break;
}
//输出节点信息
System.out.println(temp);
//将节点后移
temp=temp.next;
}
}
}
//定义HeroNode,每个HeroNode对象就是一个节点
class HeroNode{
public int no;
public String name;
public String nickname;
public HeroNode next;//指向下一个节点
//构造器
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
", nickname='" + nickname + '\'' +
'}';
}
}