LinkedList | Learn for Master
  • Leetcode – Remove Nth Node From End of List (Java)

    Remove Nth Node From End of List

    Given a linked list, remove the nth node from the end of list and return its head.

    For example,

       Given linked list: 1->2->3->4->5, and n = 2.
    
       After removing the second node from the end, the linked list becomes 1->2->3->5.
    

    Note:
    Given n will always be valid.
    Try to do this in one pass.

    Analysis:

    The key to solve this problem is to use two pointers: pre and cur. The pre pointer moves N steps in the first place,

    [Read More...]
  • Leetcode – LRU cache solution in Java

    LRU cache figure

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

    get(key) – Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
    set(key, value) – Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

    Analysis

    The popular method to implement an LRU cache is to use a bounded Queue. 

    The key to for implementing an LRU is to put any recently used data at the head of the queue.

    [Read More...]
  • Learn Python List by Examples

    Python List is a powerful data structure that you almost need it in every program. Python List can be used to hold a collection of items which can  be any type. In this post, I will use examples to describe how to use python List. 

    Python List Declaration

    It is easy to create a python list object. The following code declare a List L and initialize it with 4 values.

    In [1]:

    L = [1, 2, 'Jim', ('hello, word')]
    print L
    

     

    [1, 2, 'Jim', 'hello, word']
    

     

    Data look up in python List

    We can locate the elements in the List by index.

    [Read More...]