Java链表——插入和删除

17 篇文章 0 订阅
4 篇文章 0 订阅

目录

一、head头节点

二、插入

0.长度

1.头插法

2.尾插法

3.按位置插入

三、删除

四、完整的链表类代码


首先引入我自定义建立链表节点类:ListNode

public class ListNode {
	private int val;
	private ListNode next;
	public ListNode(int value) {
		this.val = value;
	}
	public ListNode() {}
	public ListNode getNext() {
		return this.next;
	}
	public int getVal() {
		return this.val;
	}
	public void setNext(ListNode next) {
		this.next = next;
	}
    public void setVal(int value) {
		this.val = value;
	}
}

一、head头节点

        新建一个类LinkList,链表类。

        注意,为了实现链表的各种操作,我们定义一个ListNode 类的对象head。称为头节点,用于指向链表的起点。LinkNode是我自定义的类,用于实现链表对象的建立。

        定义head:

ListNode head = null;

二、插入

0.长度

        在正式写插入之前,引入一个变量 length 表示链表的长度。

        每执行一次插入,就让 length + 1。

        每执行一次删除,就让 length - 1。

private int length = 0;
public int getLength() {
	return length;
}

链表的插入也即链表的构建,把点连成链。因插入位置不同分成三种情况。

1.头插法

        在链表最前端插入数据:

public void HeadInsert(int val) {
		ListNode newNode = new ListNode(val);    //首先把val建立成一个新节点
		if(head == null) {    //链表为空的情况
			head = newNode;
			this.length += 1; //记录长度
			return;
		}
		newNode.setNext(head);    //链表不为空,则把原第一个节点给到新节点的next
		head = newNode;    //新节点成为头节点
		this.length += 1;  //记录长度
	}

2.尾插法

        在链表最后插入数据

public void EndInsert(int val) {
		//新建节点存储数据
		ListNode newNode = new ListNode(val); 
		//判断头节点是否为空,就是链表是否为空
		if(head == null) {
			head = newNode;
			this.length += 1;  //记录长度
			return;
		}
		ListNode indexNode = head;    //由于头节点head不能改变,召唤替身
		while (indexNode.getNext() != null) {    //从头向后遍历,直到某一节点的next
			indexNode = indexNode.getNext();    //是空的,意味他是最后一个节点。
		}
		indexNode.setNext(newNode);    //让原来最后一个节点的next指向新节点
		this.length += 1;  //记录长度
	}

3.按位置插入

当在链表中间插入值的时候,新节点:new

原插入位置节点:temp

temp前一个节点:pre

插入操作需要做的:

  1. new.next = temp
  2. pre.next = new
public void Insert(int val,int index) {//数值,插入位置
        //当然this.getLength() 可以换成 this.length
		if(index<0 || index > this.getLength()) {
			System.out.println("index位置不合法");
            return;
		}
		if(index == 0) {
			HeadInsert(val);
		}else if(index == this.getLength()) {
			EndInsert(val);
		}else {
			ListNode newNode = new ListNode(val);
			ListNode tempNode = head;
			ListNode preNode = null;   // pre在temp前1位
			int position = 0;    //通过它找到正确插入位置
			while (tempNode != null) {
				if(position == index) {
					newNode.setNext(tempNode);    //pre在temp前1位  
              //temp指向的是要被插入的位置的值
					preNode.setNext(newNode);
					this.length += 1;    //记录长度
					return;
				}
				preNode = tempNode;
				tempNode = tempNode.getNext();
				position++;
			}
		}
	}

三、删除

        这里详细介绍的是按位置删除,与按位置插入类似。

欲删除位置节点:temp

temp前一个节点:pre

删除需要做的:

  1. pre.next = temp.next
  2. temp.next = null (temp的值会自动被回收)
public void Delete(int index) {
		if (index < 0 || index > this.getLength()) {
			System.out.println("位置不合法");
            return;
		}
		if(index == 0) {    //删除第一个节点时
			head = head.getNext();
			this.length -= 1;    //长度减 1
			return;
		}
		ListNode tempNode = head;
		ListNode preNode = null;
		int position = 0;
		while(tempNode != null) {
			if(position == index) {
                //pre和temp差1位,temp指向的是要被删除的值
				preNode.setNext(tempNode.getNext());      
                //temp的下一个值由pre和temp都指向,删除temp的                                       
				tempNode.setNext(null );
				this.length -= 1;     //长度减 1
				return;
			}
			preNode = tempNode;
			tempNode = tempNode.getNext();
			position++;
		}
	}

四、完整的链表类代码

public class LinkNode {
	ListNode head = null;
	private int length = 0;
    public int getLength() {
		return length;
	}
	public void HeadInsert(int val) {
		ListNode newNode = new ListNode(val);    //首先把val建立成一个新节点
		if(head == null) {    //链表为空的情况
			head = newNode;
			this.length += 1;
			return;
		}
		newNode.setNext(head);    //链表不为空,则把原第一个节点给到新节点的next
		head = newNode;    //新节点成为头节点
		this.length += 1;
	}
	public void EndInsert(int val) {
		//新建节点存储数据
		ListNode newNode = new ListNode(val); 
		//判断头节点是否为空,就是链表是否为空
		if(head == null) {
			head = newNode;
			this.length += 1;
			return;
		}
		ListNode indexNode = head;    //由于头节点head不能改变,召唤替身
		while (indexNode.getNext() != null) {    //从头向后遍历,直到某一节点的next
			indexNode = indexNode.getNext();    //是空的,意味他是最后一个节点。
		}
		indexNode.setNext(newNode);    //让原来最后一个节点的next指向新节点
		this.length += 1;
	}
	public void Insert(int val,int index) {//数值,插入位置
        //当然this.getLength() 可以换成 this.length
		if(index<0 || index > this.getLength()) {
			System.out.println("index位置不合法");
            return;
		}
		if(index == 0) {
			HeadInsert(val);
		}else if(index == this.getLength()) {
			EndInsert(val);
		}else {
			ListNode newNode = new ListNode(val);
			ListNode tempNode = head;
			ListNode preNode = null;   // pre在temp前1位
			int position = 0;    //通过它找到正确插入位置
			while (tempNode != null) {
				if(position == index) {
					newNode.setNext(tempNode);    //pre在temp前1位  
              //temp指向的是要被插入的位置的值
					preNode.setNext(newNode);
					this.length += 1;
					return;
				}
				preNode = tempNode;
				tempNode = tempNode.getNext();
				position++;
			}
		}
	}
	public void Delete(int index) {
		if (index < 0 || index > this.getLength()) {
			System.out.println("位置不合法");
            return;
		}
		if(index == 0) {    //删除第一个节点时
			head = head.getNext();
			this.length-=1;
			return;
		}
		ListNode tempNode = head;
		ListNode preNode = null;
		int position = 0;
		while(tempNode != null) {
			if(position == index) {
                //pre和temp差1位,temp指向的是要被删除的值
				preNode.setNext(tempNode.getNext());      
                //temp的下一个值由pre和temp都指向,删除temp的                                       
				tempNode.setNext(null );
				this.length-=1;
				return;
			}
			preNode = tempNode;
			tempNode = tempNode.getNext();
			position++;
		}
	}
}

  • 7
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值