1、链表数据结构

内存利用率高;动态分配

2、链表类定义

单向链表节点

public calss ListNode {

  int val =0;

  ListNode next = null;

  public void Node(int val_) {

    this.val = val_;

    this.next = null;

  }

}

单向链表类:

public class LinkedList {

  private ListNode head = null;

  private ListNode tail = null;

  private int size = 0;

  public void LinkedList() {

    head = null;

    tail = null;

    size = 0;

  }

}

LinkedList构造函数用默认构造函数就可以,可以不实现。

3、链表的操作

1)添加add,2)按索引删除remove,3)取值get,4)修改set,5)按值删除removeByValue

public class LinkedList {

  private ListNode head = null;

  private ListNode tail = null;

  private int size = 0;

  /////////////////////////////getEntry//////////////////////////////

  public ListNode getEntry(int index) {

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

      return null;//或者抛出异常

    }

    ListNode cur = head;

    while (index-- != 0) {

      cur = cur.next;

    }

    return cur;

  }

  ///////////////////add////////////////////

  public void add(int index, int value) {

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

      return;//抛出异常

    }

    size++;

    LiseNode newNode  = new ListNode(value);

    if (index == 0) {

      newNode.next = head.next;

      head.next = newNode;

      return;

    }

    ListNode pre = getEntry(index - 1);

    newNode.next = pre.next;

    pre.next  = newNode;

  }

  //////////////////////remove////////////////////////

  public void remove(int index) {

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

      return;///抛出异常

    }

    size--;

    if(index == 0) {

      head = head.next;

      return;

    }

    ListNode pre = getEntry(index - 1);

    pre.next = pre.next.next;

  }

  ////////////////////////get////////////////////

  public int get(int index) {

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

      return 0;///抛出异常

    }

    ListNode cur = getEntry(index);

    return cur.val;

  }

  /////////////////////set/////////////////////////////

  public void set(int index, int value) {

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

      return;//抛出异常

    }  

    ListNode cur = getEntry(index);

    cur.val = value;

  }

  /////////////////////////removeByValue/////////////////////////////

  public void removeByValue(int value) {

    ///暂时先不实现了。找到所有值相等的节点删除。

  }

}

可以使用dummy节点来消除head节点无前驱的问题。

public void add(int index, int value) {

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

    return;///抛出异常

  }

  size++;

  ListNode dummy = new ListNode();

  dummy.next = head;

  LiseNode pre = dummy;

  while (index-- != 0) {

    pre = pre.next;

  }

  ListNode newNode = new ListNode(value);

  newNode.next = pre.next;

  pre.next = newNode;

  head = dummy.next;

}

public void remove(int index) {

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

    return;//抛出异常

  }  

  size--;

  ListNode dummy = new ListNode(-1);

  dummy.next = head;

  ListNode pre = dummy;

  while (i-- != 0) {

    pre = pre.next;

  }

  pre.next = pre.next.next;

  head = dummy.next;

}

3、链表应用count related

1)linkedlist length

public int getLength(ListNode head) {

  ListNode cur = head;

  int length = 0;

  while(cur != null) {

    length++;

    cur = cur.next;

  }

  return length;

}

2)Kth node from the end

Given a linked list,  return the kth node from the end. Linked list will never beempty and k will always be valid.

Examples:

Input: 1->4->2->3, 2

Output: 2

Input: 3->5->9->6->8, 3

Output: 9

  k is counted from 0 or 1?

method 1:

public LinkedList getTargetNode(ListNode head, int k) {

  int length = 0;

  length = getLength(head);

  ListNode cur = head;

  for (int i = 0; i < length - k; i++) {

    cur = cur.next;

  }

  return cur;

}

时间复杂度:n + n - k = 2 * n - k

method 2:

public ListNode getTargetNode(ListNode head, int k) {

  ListNode curForward = head;

  ListNode curPost = head;

  int i = 0;

  while (i < k) {

    curForward = curForward.next;

    i++;

  }

/***

*while (k-- != 0) {curForward = curForward.next}

***/

  while (curForward != null) {

    curForward = curForward.next;

    curPost = curPost.next;

  }

  return curPost;

}

时间复杂度:k + 2 * (n-k) = 2 * n - k

3、middle node

Given a linked list,  return the middle node. Linked list will never be empty.

Examples:

Input: 1->4->2->3

Output: 4

Input: 3->5->9->6->8

Output: 9

method 1:

public ListNode getMiddleNode(ListNode head) {

  ListNode cur = head;

  int length = getLength(head);

  int i = (length - 1) / 2;

  while (i-- != 0) {

    cur = cur.next;

  }

  return cur;

}

method 2:

public ListNode getMiddleNode(ListNode head) {

  ListNode fast = head;

  ListNode slow = head;

  while (fast.next != null && fast.next.next != null) {

    fast = fast.next;

    slow = slow.next;

  }

  return slow;

}

4、Linked List Cycle

Given a linked list,  define if there is a cycle in it.

Examples:

Input: 1->4->2->3

Output: false.

Input: 3->5->9->3(original)

Output: true

public boolean findListCycle(ListNode head) {

  ListNode fast = head;

  ListNode slow = head;

  while (fast != null && fast.next != null) {

    if (fast.next == slow) {/******中间差一步时,下一个循环是slow走一步,fast走两步,相遇*******/

      return true;

    }

    slow = slow.next;

    fast = fast.next.next;

  }

  return false;

}

链表的环入口节点分析:http://blog.csdn.net/cyuyanenen/article/details/51712420

linkedlist--lecture-4的更多相关文章

  1. [C2P3] Andrew Ng - Machine Learning

    ##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...

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

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

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

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

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

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

  5. 用JavaScript来实现链表LinkedList

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

  6. ArrayList LinkedList源码解析

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

  7. ArrayList、Vector、LinkedList的区别联系?

    1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...

  8. LinkedList<E>源码分析

    LinkedList的数据结构就是双向链表,如下所示: private static class Node<E> { E item;//数据元素 Node<E> next;// ...

  9. Java集合之LinkedList

    一.LinkedList概述 1.初识LinkedList 上一篇中讲解了ArrayList,本篇文章讲解一下LinkedList的实现. LinkedList是基于链表实现的,所以先讲解一下什么是链 ...

  10. 集合 LinkedList、ArrayList、Set、Treeset

    LinkedList中特有的方法: 1:方法介绍 addFirst(E e) addLast(E e) getFirst() getLast() removeFirst() removeLast() ...

随机推荐

  1. POJ 1664 放苹果(递归或DP)

    一.Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t ...

  2. delphi 线程教学第二节:在线程时空中操作界面(UI)

    第二节:在线程时空中操作界面(UI)   1.为什么要用 TThread ?   TThread 基于操作系统的线程函数封装,隐藏了诸多繁琐的细节. 适合于大部分情况多线程任务的实现.这个理由足够了吧 ...

  3. mybatis 学习三 mapper xml 配置信息

    mapper xml 映射文件 1,select 标签      简单是用就这样,其中resultType 代表从这条语句中返回的期望类型的类的完全限定名或别名.也可以使用resultMap对应的id ...

  4. play的job执行方式

    除了使用Quartz CRON trigger, 还可以写一个action来专门触发job,这样子就可以随时启动job的开始,而且还能并行其他的任务.较方便.

  5. Could not get lock /var/lib/dpkg/lock - open 解决方法

    无法获得锁 /var/lib/dpkg/lock E: Could not get lock /var/lib/dpkg/lock - open (11 Resource temporarily un ...

  6. Hive之数据查询

    Hive之数据查询 发布于:2013 年 10 月 11 日 由 Aaron发布于: Hive 一,排序和聚合 对于排序有两种方式,一种是order by 一种是sort by order by 会对 ...

  7. 数学建模美赛O奖论文总结

    Anil S. Damle Colin G. West Eric J. Benzel University of Colorado–Boulder Boulder, CO Advisor: Anne ...

  8. Codeforces Round #522 Div2C(思维)

    #include<bits/stdc++.h>using namespace std;int a[200007];int b[200007][7];int ans[200007];int ...

  9. CodeForces 116C 【BFS】

    思路: 求所有树的最大高度? 注意:所有树从树根开始? #include <bits/stdc++.h> using namespace std; typedef long long LL ...

  10. smix到底是个啥?Perl的正则表达式匹配模式

    最近在研究一个perl项目,临时学习了一下perl语法,强行看项目源码.因为总是见到各种正则表达式后面接smxi之类,虽然知道是匹配模式,但脑子里毫无概念.所以特地去学习了一下. 以上为背景. Per ...