Linked List

Singly Linked List

Find Operation

It takes us O(N) time on average to visit an element by index, where N is the length of the linked list.

Add Operation

Add a Node after a Given Node

If we want to add a new value after a given node prev, we should:

Initialize a new node cur with the given value;

Link the "next" field of cur to prev's next node next;

Link the "next" field in prev to cur.

O(1) time, space complexity

@LeetCode

Add a Node at the Beginning

So it is essential to updateheadwhen adding a new node at the beginning of the list.

  1. Initialize a new node cur;

  2. Link the new node to our original head node head.

  3. Assign cur to head.

Delete Operation

Doubly Linked List

Doubly Linked List Node Structure

Linked List Basic Operations and Classic Problems

Based on Java, Singly Linked List

ListNode Class

Reverse a Linked List

Ref: https://leetcode.com/articles/reverse-linked-list/

iterative

recursive

Find the Middle Point

If there are two middle nodes, return the second middle node.

If there are two middle nodes, return the first middle node.

Find the Nth Element

Dummy Node

Merge Two Sorted Lists

Iterative

Recursive

Remove Linked List Elements

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

Iterative

Recursive

Linked List Has Cycle

Two Pointer Technique in Linked List 链表中的两个指针

For Linked List, we can use two-pointer technique:

Two pointers are moved at different speed: one is faster while another one might be slower.

The scenario, which is also called slow-pointer and fast-pointer technique, is really useful.

Detect Cycles in Linked List

If there is no cycle, the fast pointer will stop at the end of the linked list.

If there is a cycle, the fast pointer will eventually meet with the slow pointer.

The proper speed for the two pointers?

Slow pointer - One step at a time

Fast pointer - Two steps at a time

链表中快慢指针的Template

Tips

1. Always examine if the node is null before you call the next field.

总是检查节点是否为null,否则node.next会导致null-pointer错误

Getting the next node of a null node will cause the null-pointer error. For example, before we run fast = fast.next.next, we need to examine both fast and fast.next is not null.

2. Carefully define the end conditions of your loop.

仔细检查循环结束的条件,避免出现死循环

Run several examples to make sure your end conditions will not result in an endless loop. And you have to take our first tip into consideration when you define your end conditions.

LinkedList vs ArrayList

Hierarchy Diagram in Collection

Performance Comparison of ArrayList vs LinkedList

Reverse List in Java

https://www.techiedelight.com/reverse-arraylist-java/

https://www.geeksforgeeks.org/collections-reverse-java-examples/

Here we provide a comparison oftime complexitybetween the linked list and other data structures includingarray, queue and stack:

After this comparison, it is not difficult to come up with our conclusion:

If you need to add or delete a node frequently, a linked list could be a good choice.

If you need to access an element by index often, an array might be a better choice than a linked list.

Last updated

Was this helpful?