博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linked list
阅读量:3950 次
发布时间:2019-05-24

本文共 2474 字,大约阅读时间需要 8 分钟。

package util;import java.util.NoSuchElementException;/** * @Author lyr * @create 2020/6/28 13:13 */public class LinkedList {
static class Node {
private int value; private Node next; public Node(int value) {
this.value = value; } } private Node first; private Node last; /** * addFirst * addLast * deleteFirst * deleteLast * contains * indexOf */ public void addLast(int item) {
Node n = new Node(item); if (isEmpty()) {
first = last = n; } else {
last.next = n; last = n; } } public void addFirst(int item) {
Node node = new Node(item); if (isEmpty()) {
first = last = node; } else {
node.next = first; first = node; } } public int indexOf(int item) {
int index = 0; Node current = first; while (current != null) {
if (current.value == item) return index; index++; current = current.next; } return -1; } public boolean contains(int item) {
return indexOf(item) != -1; } public void removeFirst() {
if (isEmpty()) {
throw new NoSuchElementException(); } if (first == last) {
first = last = null; } // [10,20,30] //first ->20 Node second = first.next; //移除连接 first.next = null; first = second; } /** * remove the last element */ public void removeLast() {
if (isEmpty()) {
throw new NoSuchElementException(); } if (first == last) {
first = last = null; return; } Node previous = getPrevious(last); last = previous; last.next = null; } private Node getPrevious(Node node) {
Node current = first; while (current != null) {
if (current.next == node) return current; current = current.next; } return null; } private boolean isEmpty() {
return this.first == null; }}

后续补充

public int getKthFromTheEnd(int k) {
if(isEmpty()) throw new IllegalStateException(); var a = first; var b = a; for(var i = 0;i
"); it = it.next; } return sb.append("null").toString(); }

转载地址:http://vhuzi.baihongyu.com/

你可能感兴趣的文章
ES7.6.2安装
查看>>
查看jar依赖树
查看>>
idea运行gradle项目
查看>>
es安装ltr插件
查看>>
开源ltr-es-7.6.2代码到本地idea打开出现各种错误总结
查看>>
Requests实践详解&& python通过连接开启https的elasticsearch7 服务器
查看>>
ES查询流程源码解析
查看>>
ldaps与ldap over TLS
查看>>
jvm为什么把-Xms和-Xmx的值设置成一样
查看>>
2021-01-21对map进行key或者value排序
查看>>
ConcurrentHashMap 1.7和1.8的区别
查看>>
阻塞锁与自旋锁
查看>>
【面试官:select语句和update语句分别是怎么执行的
查看>>
scala学习之安装问题
查看>>
LDAP常见错误码
查看>>
linux yum安装rpm包出现问题
查看>>
idea编译报错类似xxx.java:[85,65] 错误: 找不到符号
查看>>
ArrayList复制
查看>>
idea打开项目时,文件左下角显示橙色J
查看>>
SQL注入
查看>>