public class SinglyListNode {

int val;

SinglyListNode next;

SinglyListNode() {

}

SinglyListNode(int x) {

this.val = x;

}

}

/*执行用时:

12 ms

, 在所有 Java 提交中击败了

66.93%

的用户

内存消耗:

39.5 MB

, 在所有 Java 提交中击败了

5.06%

的用户

*/

class MyLinkedList {

int size; // 0 -- n

//SinglyListNode head = new SinglyListNode(); // 此种写法错误,链表遍历时最后会输出0

SinglyListNode head = null;

/** Initialize your data structure here. */

public MyLinkedList() {

}

/*public int length() {

int length = 0;

SinglyListNode p = head;

while (p != null) {

length ++;

p = p.next;

}

return length;

}*/

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */

public int get(int index) {

if (index < 0 || index >= size) {

return -1;

}

SinglyListNode p;

p = head;

if (index != 0) {

for (int i = 0; i < index; i++) {

p = p.next;

}

}

return p.val;

}

/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */

public void addAtHead(int val) {

SinglyListNode listNode = new SinglyListNode(val); // 创建加入的节结点

if (head != null) {

listNode.next = head;// 新节点的指针域指向当前链表的头节点

}

head = listNode; // 将插入的节点作为新的头节点

size ++;

}

/** Append a node of value val to the last element of the linked list. */

public void addAtTail(int val) {

SinglyListNode listNode = new SinglyListNode(val);

if(head == null) {

head = listNode;

size ++;

// listNode.next = null; // 冗余

return;

}

SinglyListNode p = head;

/*while (p != null) {

if (p.next == null) {

p.next = listNode;

listNode.next = null;

p = null; //循环终止条件

}else {

p = p.next;

}

}*/

while (p.next != null) {

p = p.next; // 遍历到尾节点位置

}

p.next =listNode;

size ++;

}

/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */

public void addAtIndex(int index, int val) {

if (index == size) {

addAtTail(val);

return;

}

if (index <= 0) {

addAtHead(val);

return;

}

if (index > size - 1) {

return;

}

SinglyListNode listNode = new SinglyListNode(val);

SinglyListNode p = head;

// 遍历到节点要插入位置的前一个位置

while (index - 1 > 0) {

p = p.next;

index --;

}

listNode.next = p.next;

p.next = listNode;

size ++;

}

/** Delete the index-th node in the linked list, if the index is valid. */

public void deleteAtIndex(int index) {

if (index < 0 || index >= size) {

return;

}

if (index == 0) {

head = head.next;

size --;

return;

}

SinglyListNode p = head;

SinglyListNode tempNode;

while (index - 1 > 0) {

p = p.next;

index --;

}

tempNode = p.next.next;

p.next = tempNode;

size --;

}

}

java 马克思_单链表-Java相关推荐

  1. java单链表节点翻转_单链表Java实现

    ​ 链表中的结点是以结点来表示,单链表每一个结点有一个指针域和data域,物理位置不是连续的,逻辑上是连续的. 代码实现 class LinkedList<E> {private Node ...

  2. 双链表java实现_双链表 Java实现数据结构

    创建双链表DoubleLinkedList,带范型 public class DoubleLinkedList { private Node head; private Node tail; priv ...

  3. 浅谈:数据结构之单链表,java代码演示单链表

    单链表 本文是观看尚硅谷韩老师视频学习总结,部分来源网络. 单链表介绍 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每 ...

  4. [Java数据结构][3]单链表以及双向链表Java代码实现

    单链表Java代码实现,以水浒英雄链表为例 文章目录 单链表Java代码实现,以水浒英雄链表为例 定义一个英雄链表 定义一个SingleLinkedList 用于管理结点 初始化头结点以及添加结点到单 ...

  5. java语言实现单链表---不含头结点

    java语言实现单链表---不含头结点 一.相关概念 1.什么是线性表 2.什么是顺序表 3.什么是链表 4.单链表.双链表.循环单链表.循环双链表 5.头结点和首结点 6.常见的栈和队列与线性表的关 ...

  6. Java数据结构——用单链表编写一个简易通讯录

    Java数据结构--用单链表编写一个简易通讯录 1.定义线性表的抽象数据类型(接口) 2.定义单链表的结点Node类 3.定义数据域中的联系人Person类 4.编写顺序表(类) 5.编写测试程序(m ...

  7. java链式结构_(Java)单链表Java语言链式结构实现(数据结构四)

    1.迭代器接口实现 package com.zhaochao; public interface Iterator { boolean hasNext(); E next(); boolean del ...

  8. java线性表合并_单链表的合并(Java实现)

    C语言能做的,Java照样可以做.但是没有指针的操作,可能使人看起来"不舒服".这个博客是为了个人学习算法和数据结构而开的,以后凡是涉及到这方面的内容,我会尽量用Java和C同时实 ...

  9. 删除链表的中间节点 Java实现_【链表问题】删除单链表的中间节点

    前言 以专题的形式更新刷题贴,欢迎跟我一起学习刷题,相信我,你的坚持,绝对会有意想不到的收获.每道题会提供简单的解答,如果你有更优雅的做法,欢迎提供指点,谢谢. [题目描述] 给定链表的头节点head ...

最新文章

  1. MyBatis 的这些坑你有踩过吗?
  2. DGA域名可以是色情网站域名
  3. 外卖ERP管理系统(一)
  4. 使用C#删除一个字符串数组中的空字符串
  5. docker 部署_GitLab CI 自动部署netcore web api 到Docker
  6. 1.1 算法编译与调试
  7. CREO 6.0 - 基础 - 01 - 零件 - 零件的装配 - 零件的移动、偏转、角度角度设定
  8. Java||求集合数组中的中位数
  9. 工作,到底意味着什么
  10. mysql安装教程 mac_Mac下MySQL安装配置教程
  11. 超越美国!中国AI初创企业融资额全球第一 人脸识别最受热捧【附报告全文】
  12. GB28181 协议实现H5无插件RTMP/HTTP-FLV/HLS直播监控方案 - sfxlab
  13. 计算机之父冯·诺依曼
  14. c语言中输入scanf与getchar的啃爹之处
  15. 生财有术:及时止损,及时止损,及时止损
  16. python的re_research是什么意思_research在线翻译_英语_读音_用法_例句_海词词典
  17. 2018几大主流的UI/JS框架——前端框架
  18. ios 保存html,iOS 9“保存PDF到iBooks”与HTML
  19. 足坛诗人--贺炜的5大经典解说
  20. 追求思考的深度与条理清晰地阐述

热门文章

  1. android访问重定向地址,如何从android中重定向url加载图像(示例代码)
  2. 双继承_在Python中使用双下划线防止类属性被覆盖!
  3. 全国计算机一级书红色封面,年度最强的网红录取通知书,竟然附赠了一张黑胶唱片...
  4. 影响索引的mysql函数_mysql索引对排序的影响实例分析
  5. 软件工程(可行性研究讲解)
  6. Javascript基础之-原型(prototype)
  7. 苏宁 11.11:仓库内多 AGV 协作的全局路径规划算法研究
  8. 【虚拟化实战】存储设计之一存储类型
  9. 利用FSMT进行文件服务器迁移及整合
  10. c#中使用多线程访问winform中控件的若干问题