算法学习——链表(一)

167 阅读3分钟

这是我参与更文挑战的第16天,活动详情查看:更文挑战

简介

  链表也是线性数据结构,但与数组不同的是,链表底层存储单元是非连续的。链表的顺序性依靠指针实现,在结构上,一个链表节点通常包括数据域指针域。单链表尤指指针域只有一个指针,这个指针指向节点的后驱节点的链表。以下是单链表的经典定义。

// C++版本
// Definition for singly-linked list.
struct SinglyListNode {
    int val;
    SinglyListNode *next;
    SinglyListNode(int x) : val(x), next(NULL) {}
};
// java版本
// Definition for singly-linked list.
public class SinglyListNode {
    int val;
    SinglyListNode next;
    SinglyListNode(int x) { val = x; }
}

单链表结构示意图:

单链表.png

单链表常见操作

在链表中间添加节点

  • 1、使用给定值初始化新结点 cur

链表添加节点_1.png

  • 2、将 curnext 字段链接到 prev 的下一个结点 next

链表添加节点_2.png

  • 3、将 prev 中的 next 字段链接到 cur

链表添加节点_3.png

  与数组不同,我们不需要将所有元素移动到插入元素之后。因此,您可以在 O(1) 时间复杂度中将新结点插入到链表中,这非常高效。但是在实际操作中,通常需要使用遍历链表的方式寻找插入的位置,而这个搜索过程的时间复杂度为O(n)。

在表头添加节点

  • 初始化一个新结点 cur
  • 将新结点链接到我们的原始头结点 head
  • cur 指定为 head

  与在中间添加节点不一样的是,在表头添加节点没有搜索过程,整个时间复杂度只有插入节点的消耗,因此是O(1)。

删除节点

  • 找到 cur 的上一个结点 prev 及其下一个结点 next ;

删除节点_1.png

  • 接下来链接 prev 到 cur 的下一个节点 next 。

删除节点_2.png

  在我们的第一步中,我们需要找出 prev 和 next。使用 cur 的参考字段很容易找出 next,但是,我们必须从头结点遍历链表,以找出 prev,它的平均时间是 O(N),其中 N 是链表的长度。因此,删除结点的时间复杂度将是 O(N)。

  空间复杂度为 O(1),因为我们只需要常量空间来存储指针。

查找节点

  查找节点一般分为:查找指定位置节点内容查找指定元素的节点位置。查找指定元素的节点位置,不用说,肯定需要通过遍历链表完成,因此时间复杂的为O(n)。由于链表的底层存储并不是连续的,因此无法通过索引(index)进行查找,对于查找指定位置的节点内容,链表只能通过表头一个个遍历查找,其时间复杂度也是O(n)。

设计链表

在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
public class ListNode {
  int val;
  ListNode next;
  ListNode(int x) { val = x; }
}

class MyLinkedList {
  int size;
  ListNode head;  // sentinel node as pseudo-head
  public MyLinkedList() {
    size = 0;
    head = new ListNode(0);
  }

  /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
  public int get(int index) {
    // if index is invalid
    if (index < 0 || index >= size) return -1;

    ListNode curr = head;
    // index steps needed 
    // to move from sentinel node to wanted index
    for(int i = 0; i < index + 1; ++i) curr = curr.next;
    return curr.val;
  }

  /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
  public void addAtHead(int val) {
    addAtIndex(0, val);
  }

  /** Append a node of value val to the last element of the linked list. */
  public void addAtTail(int val) {
    addAtIndex(size, val);
  }

  /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
  public void addAtIndex(int index, int val) {
    // If index is greater than the length, 
    // the node will not be inserted.
    if (index > size) return;

    // [so weird] If index is negative, 
    // the node will be inserted at the head of the list.
    if (index < 0) index = 0;

    ++size;
    // find predecessor of the node to be added
    ListNode pred = head;
    for(int i = 0; i < index; ++i) pred = pred.next;

    // node to be added
    ListNode toAdd = new ListNode(val);
    // insertion itself
    toAdd.next = pred.next;
    pred.next = toAdd;
  }

  /** Delete the index-th node in the linked list, if the index is valid. */
  public void deleteAtIndex(int index) {
    // if the index is invalid, do nothing
    if (index < 0 || index >= size) return;

    size--;
    // find predecessor of the node to be deleted
    ListNode pred = head;
    for(int i = 0; i < index; ++i) pred = pred.next;

    // delete pred.next 
    pred.next = pred.next.next;
  }
}