Linked List - From LeetCode.com

Introduction

Similar to the array, the linked list is also a linear data structure. Here is an example:
这里写图片描述

As you can see, each element in the linked list is actually a separate object while all the objects are linked together by the reference field in each element.

There are two types of linked list: singly linked list and doubly linked list. The example above is a singly linked list and here is an example of doubly linked list:
这里写图片描述
We will introduce more in later chapters. After this card, you will:

  1. Understand the structure of singly linked list and doubly linked list;
    Implement traversal, insertion, deletion in a singly or doubly linked list;
  2. Analyze the complexity of different operations in a singly or doubly linked list;
  3. Use two-pointer technique (fast-pointer-slow-pointer technique) in the linked list;
  4. Solve classic problems such as reverse a linked list;
  5. Analyze the complexity of the algorithms you designed;
  6. Accumulate experience in designing and debugging.

Delete Operation - Singly Linked List

if we want to delete an existing node cur from the singly linked list, we can do it in two steps:

1.Find cur’s previous node prev and its next node next;
这里写图片描述
2.Link prev to cur’s next node next.
这里写图片描述

In our first step, we need to find out prev and next. It is easy to find out next using the reference field of cur. However, we have to traverse the linked list from the head node to find out prev which will take O(N) time on average, where N is the length of the linked list. So the time complexity of deleting a node will be O(N).

The space complexity is O(1) because we only need constant space to store our pointers.


An Example
这里写图片描述
Let’s try to delete node 6 from the singly linked list above.

  1. Traverse the linked list from the head until we find the previous node prev which is node 23

  2. Link prev (node 23) with next (node 15)
    这里写图片描述
    Node 6 is not in our singly linked list now.


Delete the First Node

If we want to delete the first node, the strategy will be a little different.

As we mentioned before, we use the head node head to represent a linked list. Our head is the black node 23 in the example below.
这里写图片描述
If we want to delete the first node, we can simply assign the next node to head. That is to say, our head will be node 6 after deletion.
这里写图片描述
The linked list begins at the head node, so node 23 is no longer in our linked list.


Add Operation - Singly Linked List

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

  1. Initialize a new node cur with the given value;
    这里写图片描述
  2. Link the “next” field of cur to prev’s next node next;
    这里写图片描述
  3. Link the “next” field in prev to cur.
    这里写图片描述
    Unlike an array, we don’t need to move all elements past the inserted element. Therefore, you can insert a new node into a linked list in O(1) time complexity, which is very efficient.

An Example

Let’s insert a new value 9 after the second node 6.
这里写图片描述
We will first initialize a new node with value 9. Then link node 9 to node 15. Finally, link node 6 to node 9.

After insertion, our linked list will look like this:
这里写图片描述


Add a Node at the Beginning

As we know, we use the head node head to represent the whole list.

So it is essential to update head when 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.

For example, let’s add a new node 9 at the beginning of the list.
这里写图片描述
We initialize a new node 9 and link node 9 to current head node 23.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值