Java实现链表逻辑

/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */
    package helloworld1;

/**
*

  • @author wjx
    */
    class ListNode {
    int val;//节点的值
    ListNode next = null;//节点的指针

    ListNode(int val){
    this.val = val;
    }

}

public class HelloWorld1 {
//将一个新节点添加到链表尾部
public static void addNode(ListNode listNode, ListNode node){
//如果链表是一个空链表,则将此节点赋给链表
if (listNode == null)
listNode = node;
if (listNode != null){
while (listNode.next != null){
listNode = listNode.next;
}
listNode.next = node;
}
}
//将node2节点插入到node1后面
public static void addNode(ListNode listNode, ListNode node1, ListNode node2){
ListNode currNode;
while(listNode.next != null){
currNode = listNode.next;
if (currNode.val == node1.val){
//注意这里需要先将插入的节点的next指向链表插入位置后面
//再将插入位置的next指向插入节点
node2.next = currNode.next;
listNode.next.next = node2;
break;
}
listNode = currNode;
}
}
//删除链表中的某个值(此链表头结点不存入值)
public static void removeNode(ListNode listNode, ListNode node){
ListNode currNode;
while (listNode.next != null) {
currNode = listNode.next;
if (currNode.val == node.val){
listNode.next = currNode.next;
}
listNode = currNode;
}
}
//从头部到尾部打印出链表的值(跳过链表的头结点)
public static void showListNode(ListNode listNode){
listNode = listNode.next;
while (listNode != null){
System.out.println(listNode.val);
listNode = listNode.next;
}
}
//获取链表的长度
public static int getLenOfLNode(ListNode listNode){
int len = 0;
listNode = listNode.next;
while (listNode != null){
len++;
listNode = listNode.next;
}
return len;
}

public static void main(String[] args) {
    //创建一个头结点
    ListNode listNode = new ListNode(444);
    //创建三个节点,为后面测试使用
    ListNode l1 = new ListNode(1);
    ListNode l2 = new ListNode(5);
    ListNode l3 = new ListNode(4);

    addNode(listNode, l1);
    addNode(listNode, l2);
    addNode(listNode, l3);
    showListNode(listNode);
    System.out.println("=====================");
    System.out.print("链表的长度为:");
    System.out.println(getLenOfLNode(listNode));
    System.out.println("=====================");
    System.out.println("删除l2节点:");
    removeNode(listNode, l2);
    showListNode(listNode);
    System.out.println("=====================");
    System.out.println("插入l2节点");
    addNode(listNode, l1, l2);
    showListNode(listNode);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值