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的更多相关文章

  1. Enhancing the Scalability of Memcached

    原文地址: https://software.intel.com/en-us/articles/enhancing-the-scalability-of-memcached-0 1 Introduct ...

  2. 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 ...

  3. JDK源代码学习-ArrayList、LinkedList、HashMap

    ArrayList.LinkedList.HashMap是Java开发中非常常见的数据类型.它们的区别也非常明显的,在Java中也非常具有代表性.在Java中,常见的数据结构是:数组.链表,其他数据结 ...

  4. [CareerCup] Single Valid Tree

    https://www.careercup.com/question?id=5103530547347456 Given a list of nodes, each with a left child ...

  5. To Java程序员:切勿用普通for循环遍历LinkedList

    ArrayList与LinkedList的普通for循环遍历 对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: p ...

  6. 计算机程序的思维逻辑 (39) - 剖析LinkedList

    上节我们介绍了ArrayList,ArrayList随机访问效率很高,但插入和删除性能比较低,我们提到了同样实现了List接口的LinkedList,它的特点与ArrayList几乎正好相反,本节我们 ...

  7. 深入理解java中的ArrayList和LinkedList

    杂谈最基本数据结构--"线性表": 表结构是一种最基本的数据结构,最常见的实现是数组,几乎在每个程序每一种开发语言中都提供了数组这个顺序存储的线性表结构实现. 什么是线性表? 由0 ...

  8. 用JavaScript来实现链表LinkedList

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文地址. 写在前面 好多做web开发的朋友,在学习数据结构和算法时可能比较讨厌C和C++,上学的时候写过的也忘得差不多了,更别提没写过的了.但幸运 ...

  9. ArrayList LinkedList源码解析

    在java中,集合这一数据结构应用广泛,应用最多的莫过于List接口下面的ArrayList和LinkedList; 我们先说List, public interface List<E> ...

随机推荐

  1. 虚函数(virtual)为啥不能是static

    静态成员函数,可以不通过对象来调用,即没有隐藏的this指针. virtual函数一定要通过对象来调用,即有隐藏的this指针. static成员没有this指针是关键!static function ...

  2. Java API —— Random类

    1.Random类概述         此类用于产生随机数         如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列. 2.构造 ...

  3. apache&nginx资料汇总

    http://liudaoru.iteye.com/blog/336338 aquid:http://os.51cto.com/art/201009/225813.htm 数据库各种讲座:http:/ ...

  4. ZOJ 3790 Consecutive Blocks (离散化 + 暴力)

    题目链接 虽然是一道暴力的题目,但是思路不好想.刚开始还超时,剪枝了以后1200ms,不知道为什么还是这么慢. 题意:给你n个点,每个点有一种颜色ci,给你至多k次删除操作,每次删除一个点,问最多k次 ...

  5. 基于XMPP的即时通信系统的建立(四)— 协议详解

    Presence 在XMPP协议中,我们使用presence来获取用户是否已经上线以及是否可以通信的状态. 为了能够知道自己联系人的状态以及让联系人知道自己的状态,用户上线后需要订阅联系人的状态,联系 ...

  6. iOS开发:为xcode项目添加git仓储

    现在apple官网下载Command Line Tools 对应mac版本和xcode版本,记录地址:https://developer.apple.com/downloads/ 找到mac的终端,c ...

  7. [HUD 1195] Open the Lock

    Open the Lock Problem Description Now an emergent task for you is to open a password lock. The passw ...

  8. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...

  9. UVA 550 Multiplying by Rotation (简单递推)

    题意:有些数字是可以这样的:abcd*k=dabc,例如179487 * 4 = 717948,仅仅将尾数7移动到前面,其他都不用改变位置及大小.这里会给出3个数字b.d.k,分别代表b进制.尾数.第 ...

  10. jQuery的datatable怎么才能给某一列添加超链接?

    aocolumeDef.这个里面去定义.return返回的字符串会代替原来cell里面的内容 e.g: datatable=$('#dt_basic').dataTable({ "bAuto ...