自己实现Single LinkedList
My_Single_LinkedList
分4个部分实现(CRUD - 增删改查)。
首先要有一个Node(节点类)
     class Node {
         public int val;
         public Node next;
         public Node(int val) {
             this.val = val;
             this.next = null;
         }
     }
instance variables and constructer - 成员变量和构造函数
public Node head; //头指针指向虚拟的node,真正的list是取head.next
public Node last; //尾指针指向最后一个元素
public int length; //list的长度,不是index。在insert的时候,判断insert的index public My_Single_LinkedList() {
this.head = new Node(-1);
this.last = head;
this.length = 0;
}
1. 添加
添加部分有两大部分,一个是添加在头和尾,一个是添加在某个index的左和右。
添加的同时要保持length和last指针的更新
1) 添加在头和尾:O(1)的时间复杂度
     public void addLast(int val) {
         last.next = new Node(val);
         last = last.next;
         this.length++;
     }
     public void addFirst(int val) {
         Node newNode = new Node(val);
         newNode.next = head.next;
         head.next = newNode;
         this.length++;
     }
2) 在某个index的左和右添加:O(n)时间复杂度
/**
* insert before index
* @param val
* @param index
*/
public void insertBefore(int val, int index) {
if (index <= 0) { //在0之前加,相当于addFirst
addFirst(val);
return;
}
if (index >= length) {//在超出length之前加,相当于addLast
addLast(val);
return;
}
// 普通情况,两个指针,一前一后
Node cur = head.next;
Node prev = head;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
prev = prev.next;
counter++;
}
Node newNode = new Node(val);
newNode.next = cur;
prev.next = newNode;
this.length++;
} /**
* insert after index
* @param val
* @param index certain index you want to insert at
*/
public void insertAfter(int val, int index) {
if (index < 0) {
addFirst(val);
return;
}
if (index >= length - 1) {
addLast(val);
return;
}
Node cur = head.next;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
Node newNode = new Node(val);
newNode.next = cur.next;
cur.next = newNode;
this.length++;
}
2. 更新:O(n)时间复杂度
     public void updateAt(int val, int index) {
         if (index < 0 || index > length - 1) {
             return;
         }
         int counter = 0;
         Node cur = head.next;
         while (cur != null && counter < index) {
             cur = cur.next;
             counter++;
         }
         cur.val = val;
     }
3. 删除:O(n)时间复杂度
删除的时候也要注意length和last的更新
     public void deleteAt(int index) {
         if (index < 0 || index > length - 1) {
             return;
         }
         int counter = 0;
         Node prev = head;
         Node cur = head.next;
         while (cur != null && counter < index) {
             cur = cur.next;
             prev = prev.next;
             counter++;
         }
         prev.next = prev.next.next;
         //更新length和last指针
         length--;
         while (prev.next != null) {
             prev = prev.next;
         }
         this.last = prev;
     }
4. 搜索:O(n)时间复杂度
/**
* 根据val搜索
* 返回第一个符合条件node的index
* 如果没有,返回-1
*/
public int getByValue(int val) {
Node cur = head.next;
int index = 0; while (cur != null) {
if (cur.val == val) {
return index;
}
cur = cur.next;
index++;
}
return -1;
} /**
* 根据index搜索
*/
public Node get(int index) {
if (index < 0 || index > length - 1) {
return null;
}
Node cur = head.next;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
return cur;
}
完整代码:
/**
* Created by Mingxiao on 9/5/2016.
*/
public class My_Single_LinkedList { class Node {
public int val;
public Node next; public Node(int val) {
this.val = val;
this.next = null;
}
} public Node head; //头指针指向虚拟的node,真正的list是取head.next
public Node last; //尾指针指向最后一个元素
public int length; //list的长度,不是index。在insert的时候,判断insert的index public My_Single_LinkedList() {
this.head = new Node(-1);
this.last = head;
this.length = 0;
}
// =============== INSERT==========================
public void addLast(int val) {
last.next = new Node(val);
last = last.next;
this.length++;
} public void addFirst(int val) {
Node newNode = new Node(val);
newNode.next = head.next;
head.next = newNode;
this.length++;
} /**
* insert before index
* @param val
* @param index
*/
public void insertBefore(int val, int index) {
if (index <= 0) { //在0之前加,相当于addFirst
addFirst(val);
return;
}
if (index >= length) {//在超出length之前加,相当于addLast
addLast(val);
return;
}
// 普通情况,两个指针,一前一后
Node cur = head.next;
Node prev = head;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
prev = prev.next;
counter++;
}
Node newNode = new Node(val);
newNode.next = cur;
prev.next = newNode;
this.length++;
} /**
* insert after index
* @param val
* @param index certain index you want to insert at
*/
public void insertAfter(int val, int index) {
if (index < 0) {
addFirst(val);
return;
}
if (index >= length - 1) {
addLast(val);
return;
}
Node cur = head.next;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
Node newNode = new Node(val);
newNode.next = cur.next;
cur.next = newNode;
this.length++;
} //===================update=========================
public void updateAt(int val, int index) {
if (index < 0 || index > length - 1) {
return;
}
int counter = 0;
Node cur = head.next;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
cur.val = val;
}
//=====================delete=======================
public void deleteAt(int index) {
if (index < 0 || index > length - 1) {
return;
}
int counter = 0;
Node prev = head;
Node cur = head.next;
while (cur != null && counter < index) {
cur = cur.next;
prev = prev.next;
counter++;
}
prev.next = prev.next.next;
//更新length和last指针
length--;
while (prev.next != null) {
prev = prev.next;
}
this.last = prev;
}
//===================search================================ /**
* 根据val搜索
* 返回第一个符合条件node的index
* 如果没有,返回-1
*/
public int getByValue(int val) {
Node cur = head.next;
int index = 0; while (cur != null) {
if (cur.val == val) {
return index;
}
cur = cur.next;
index++;
}
return -1;
} /**
* 根据index搜索
*/
public Node get(int index) {
if (index < 0 || index > length - 1) {
return null;
}
Node cur = head.next;
int counter = 0;
while (cur != null && counter < index) {
cur = cur.next;
counter++;
}
return cur;
} public void print() {
if (head.next == null) {
System.out.println("null");
return;
}
Node cur = head.next;
while (cur.next != null) {
System.out.print(cur.val + "->");
cur = cur.next;
}
System.out.println(cur.val);
} public static void main(String[] args) {
My_Single_LinkedList list = new My_Single_LinkedList();
list.addLast(1);
list.addLast(2);
list.addLast(0);
list.insertAfter(5,0);
list.updateAt(9, 1);
list.deleteAt(2);
System.out.println(list.get(2).val);
list.print();
}
}
自己实现Single LinkedList的更多相关文章
- Enhancing the Scalability of  Memcached
		原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduct ... 
- cc150 Chapter 2 | Linked Lists 2.5  add two integer LinkedList, return LinkedList as a sum
		2.5 You have two numbers represented by a linked list, where each node contains a single digit. The ... 
- JDK源代码学习-ArrayList、LinkedList、HashMap
		ArrayList.LinkedList.HashMap是Java开发中非常常见的数据类型.它们的区别也非常明显的,在Java中也非常具有代表性.在Java中,常见的数据结构是:数组.链表,其他数据结 ... 
- [CareerCup] Single Valid Tree
		https://www.careercup.com/question?id=5103530547347456 Given a list of nodes, each with a left child ... 
- To Java程序员:切勿用普通for循环遍历LinkedList
		ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ... 
- 计算机程序的思维逻辑 (39) -  剖析LinkedList
		上节我们介绍了ArrayList,ArrayList随机访问效率很高,但插入和删除性能比较低,我们提到了同样实现了List接口的LinkedList,它的特点与ArrayList几乎正好相反,本节我们 ... 
- 深入理解java中的ArrayList和LinkedList
		杂谈最基本数据结构--"线性表": 表结构是一种最基本的数据结构,最常见的实现是数组,几乎在每个程序每一种开发语言中都提供了数组这个顺序存储的线性表结构实现. 什么是线性表? 由0 ... 
- 用JavaScript来实现链表LinkedList
		本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ... 
- ArrayList LinkedList源码解析
		在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ... 
随机推荐
- BZOJ 2299 向量
			题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2299 题意:给出一对数a,b,任意使用(a,b), (a,-b), (-a,b), (- ... 
- Lists of network protocols
			https://en.wikipedia.org/wiki/Lists_of_network_protocols Protocol stack: List of network protocol st ... 
- 【转】Eclipse Java注释模板设置详解
			Eclipse Java注释模板设置详解 设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后 ... 
- uva11181Probability|Given
			枚举,条件概率. 2^20次方等于100w,是大约可以没准还能过的. 二进制枚举时,如果买东西的人恰好为r个,设概率为p,就将sum[i]+=p(sum[i]为r个人买东西时第i个人买东西的概率),t ... 
- Java Web编程的主要组件技术——JSP
			参考书籍:<J2EE开源编程精要15讲> JSP(Java Server Page)页面由HTML代码和嵌入其中的Java代码组成. 简单的JSP页面如: <html> < ... 
- Android ashmem hacking
			/********************************************************************** * Android ashmem hacking * 声 ... 
- apache开源项目 -- tajo
			一.体系架构 Tajo采用了Master-Worker架构(下图虚线框目前还在计划中),Master-Worker-Client之间的RPC通信是使用Protocol buffer + Netty来实 ... 
- Java [Leetcode 319]Bulb Switcher
			题目描述: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off ... 
- 省常中模拟 Test2 Day2
			two 模拟 大意:给你一个 N 位二进制数,有四种操作:加1.减1.乘2.整除2.给定一个操作序列,求最终结果.N <= 5*10^6.数据保证不会在最高位上进行进位或退位操作. 初步解法:由 ... 
- 【C#学习笔记】读SQL Server2008
			using System; using System.Data.SqlClient; namespace ConsoleApplication { class Program { static voi ... 
