• LeetCode Linked List Cycle (java)

    Given a linked list, determine if it has a cycle in it.

    Follow up:
    Can you solve it without using extra space?

    Java Solution

    [Read More...]
  • Leetcode Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val.

    Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
    Return: 1 –> 2 –> 3 –> 4 –> 5


    To solve this problem, we keep two pointers pre and cur when scanning the LinkedList. Once the current node’s value equals to the target value, we remove the cur node by set pre.next = cur.next;

    To make the implementation easier,

    [Read More...]
  • 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.

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


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