加载中...
707-设计链表(Design Linked List)
发表于:2021-12-03 | 分类: 中等
字数统计: 3.9k | 阅读时长: 21分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/design-linked-list

英文原文

Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement the MyLinkedList class:

  • MyLinkedList() Initializes the MyLinkedList object.
  • int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.
  • void addAtHead(int 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.
  • void addAtTail(int val) Append a node of value val as the last element of the linked list.
  • void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.
  • void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.

 

Example 1:

Input
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[], [1], [3], [1, 2], [1], [1], [1]]
Output
[null, null, null, null, 2, null, 3]

Explanation
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2);    // linked list becomes 1->2->3
myLinkedList.get(1);              // return 2
myLinkedList.deleteAtIndex(1);    // now the linked list is 1->3
myLinkedList.get(1);              // return 3

 

Constraints:

  • 0 <= index, val <= 1000
  • Please do not use the built-in LinkedList library.
  • At most 2000 calls will be made to get, addAtHead, addAtTail, addAtIndex and deleteAtIndex.

中文题目

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 nextval 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

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

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

 

示例:

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
linkedList.get(1);            //返回2
linkedList.deleteAtIndex(1);  //现在链表是1-> 3
linkedList.get(1);            //返回3

 

提示:

  • 所有val值都在 [1, 1000] 之内。
  • 操作次数将在  [1, 1000] 之内。
  • 请不要使用内置的 LinkedList 库。

通过代码

官方题解

面试要点

链表时一个包含零个或多个元素的数据结构。每个元素都包含一个值和到另一个元素的链接。根据链接数的不同,可以分为单链表,双链表和多重链表。

单链表是最简单的一种,它提供了在常数时间内的 addAtHead 操作和在线性时间内的 addAtTail 的操作。双链表是最常用的一种,因为它提供了在常数时间内的 addAtHeadaddAtTail 操作,并且优化的插入和删除。

双链表在 Java 中的实现为 LinkedList,在 Python 中为 list。这些结构都比较常用,有两个要点:

  • 哨兵节点:

哨兵节点在树和链表中被广泛用作伪头、伪尾等,通常不保存任何数据。

我们将使用伪头来简化我们简化插入和删除。在接下来的两种方法中应用此方法。

  • 双链表的双向搜索:我们可以从头部或尾部进行搜索。

方法一:单链表

让我们从最简单的链表开始。

在这里插入图片描述{:width=500}

[init1-Python]
class MyLinkedList: def __init__(self): self.size = 0 self.head = ListNode(0) # sentinel node as pseudo-head
[init1-Java]
class MyLinkedList { int size; ListNode head; // sentinel node as pseudo-head public MyLinkedList() { size = 0; head = new ListNode(0); } }

哨兵节点被用作伪头始终存在,这样结构中永远不为空,它将至少包含一个伪头。MyLinkedList 中所有节点均包含:值 + 链接到下一个元素的指针。

[ListNode1-Python]
class ListNode: def __init__(self, x): self.val = x self.next = None
[ListNode1-Java]
public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } }

addAtIndexaddAtHeadaddAtTail
我们首先讨论 addAtIndex,因为伪头的关系 addAtHeadaddAtTail 可以使用 addAtIndex 来完成。

这个想法很简单:

  • 找到要插入位置节点的前驱节点。如果要在头部插入,则它的前驱节点就是伪头。如果要在尾部插入节点,则前驱节点就是尾节点。
  • 通过改变 next 来插入节点。
[add1-Python]
to_add.next = pred.next pred.next = to_add
[add1-Java]
toAdd.next = pred.next; pred.next = toAdd;

在这里插入图片描述{:width=500}

在这里插入图片描述{:width=500}

deleteAtIndex
和插入同样的道理。

  • 找到要删除节点的前驱节点。
  • 通过改变 next 来删除节点。
[delete2-Python]
# delete pred.next pred.next = pred.next.next
[delete2-Java]
// delete pred.next pred.next = pred.next.next;

在这里插入图片描述{:width=500}

get
从伪头节点开始,向前走 index+1 步。

[get1-Python]
# index steps needed # to move from sentinel node to wanted index for _ in range(index + 1): curr = curr.next return curr.val
[get1-Java]
// 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;

在这里插入图片描述{:width=500}

全部代码:

[solution1-Python]
class ListNode: def __init__(self, x): self.val = x self.next = None class MyLinkedList: def __init__(self): self.size = 0 self.head = ListNode(0) # sentinel node as pseudo-head def get(self, index: int) -> int: """ Get the value of the index-th node in the linked list. If the index is invalid, return -1. """ # if index is invalid if index < 0 or index >= self.size: return -1 curr = self.head # index steps needed # to move from sentinel node to wanted index for _ in range(index + 1): curr = curr.next return curr.val def addAtHead(self, val: int) -> None: """ 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. """ self.addAtIndex(0, val) def addAtTail(self, val: int) -> None: """ Append a node of value val to the last element of the linked list. """ self.addAtIndex(self.size, val) def addAtIndex(self, index: int, val: int) -> None: """ 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. """ # If index is greater than the length, # the node will not be inserted. if index > self.size: return # [so weird] If index is negative, # the node will be inserted at the head of the list. if index < 0: index = 0 self.size += 1 # find predecessor of the node to be added pred = self.head for _ in range(index): pred = pred.next # node to be added to_add = ListNode(val) # insertion itself to_add.next = pred.next pred.next = to_add def deleteAtIndex(self, index: int) -> None: """ Delete the index-th node in the linked list, if the index is valid. """ # if the index is invalid, do nothing if index < 0 or index >= self.size: return self.size -= 1 # find predecessor of the node to be deleted pred = self.head for _ in range(index): pred = pred.next # delete pred.next pred.next = pred.next.next
[solution1-Java]
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; } }

复杂度分析

  • 时间复杂度:
    • addAtHead: $\mathcal{O}(1)$
    • addAtIndergetdeleteAtIndex: $\mathcal{O}(k)$,其中 $k$ 指的是元素的索引。
    • addAtTail:$\mathcal{O}(N)$,其中 $N$ 指的是链表的元素个数。
  • 空间复杂度:所有的操作都是 $O(1)$。

方法二:双链表

双链表比单链表快得多,测试用例花费的时间比单链表快了两倍。但是它更加复杂,它包含了 size,记录链表元素个数,和伪头伪尾。

在这里插入图片描述

[init2-Python]
class MyLinkedList: def __init__(self): self.size = 0 # sentinel nodes as pseudo-head and pseudo-tail self.head, self.tail = ListNode(0), ListNode(0) self.head.next = self.tail self.tail.prev = self.head
[init2-Java]
class MyLinkedList { int size; // sentinel nodes as pseudo-head and pseudo-tail ListNode head, tail; public MyLinkedList() { size = 0; head = new ListNode(0); tail = new ListNode(0); head.next = tail; tail.prev = head; } }

伪头和伪尾总是存在,MyLinkedList 中所有节点都包含:值 + 指向前一个节点的指针 + 指向后一个节点的指针。

[ListNode2-Python]
class ListNode: def __init__(self, x): self.val = x self.next = None self.prev = None
[ListNode2-Java]
public class ListNode { int val; ListNode next; ListNode prev; ListNode(int x) { val = x; } }

addAtIndexaddAtHeadaddAtTail

  • 找到要插入节点的前驱节点和后继节点。如果要在头部插入节点,则它的前驱结点是伪头。如果要在尾部插入节点,则它的后继节点是伪尾。
  • 通过改变前驱结点和后继节点的链接关系添加元素。
[add2-Python]
to_add.prev = pred to_add.next = succ pred.next = to_add succ.prev = to_add
[add2-Java]
toAdd.prev = pred toAdd.next = succ pred.next = toAdd succ.prev = toAdd

在这里插入图片描述

deleteAtIndex
和插入同样的道理。

  • 找到要删除节点的前驱结点和后继节点。
  • 通过改变前驱结点和后继节点的链接关系删除元素。
[delete2-Python]
pred.next = succ succ.prev = pred
[delete2-Java]
pred.next = succ succ.prev = pred

在这里插入图片描述

get

  • 通过比较 indexsize - index 的大小判断从头开始较快还是从尾巴开始较快。
  • 从较快的方向开始。
[get2-Python]
# choose the fastest way: to move from the head # or to move from the tail if index + 1 < self.size - index: curr = self.head for _ in range(index + 1): curr = curr.next else: curr = self.tail for _ in range(self.size - index): curr = curr.prev
[get2-Java]
// choose the fastest way: to move from the head // or to move from the tail ListNode curr = head; if (index + 1 < size - index) for(int i = 0; i < index + 1; ++i) curr = curr.next; else { curr = tail; for(int i = 0; i < size - index; ++i) curr = curr.prev; }

在这里插入图片描述

全部代码:

[solution2-Python]
class ListNode: def __init__(self, x): self.val = x self.next, self.prev = None, None class MyLinkedList: def __init__(self): self.size = 0 # sentinel nodes as pseudo-head and pseudo-tail self.head, self.tail = ListNode(0), ListNode(0) self.head.next = self.tail self.tail.prev = self.head def get(self, index: int) -> int: """ Get the value of the index-th node in the linked list. If the index is invalid, return -1. """ # if index is invalid if index < 0 or index >= self.size: return -1 # choose the fastest way: to move from the head # or to move from the tail if index + 1 < self.size - index: curr = self.head for _ in range(index + 1): curr = curr.next else: curr = self.tail for _ in range(self.size - index): curr = curr.prev return curr.val def addAtHead(self, val: int) -> None: """ 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. """ pred, succ = self.head, self.head.next self.size += 1 to_add = ListNode(val) to_add.prev = pred to_add.next = succ pred.next = to_add succ.prev = to_add def addAtTail(self, val: int) -> None: """ Append a node of value val to the last element of the linked list. """ succ, pred = self.tail, self.tail.prev self.size += 1 to_add = ListNode(val) to_add.prev = pred to_add.next = succ pred.next = to_add succ.prev = to_add def addAtIndex(self, index: int, val: int) -> None: """ 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. """ # If index is greater than the length, # the node will not be inserted. if index > self.size: return # [so weird] If index is negative, # the node will be inserted at the head of the list. if index < 0: index = 0 # find predecessor and successor of the node to be added if index < self.size - index: pred = self.head for _ in range(index): pred = pred.next succ = pred.next else: succ = self.tail for _ in range(self.size - index): succ = succ.prev pred = succ.prev # insertion itself self.size += 1 to_add = ListNode(val) to_add.prev = pred to_add.next = succ pred.next = to_add succ.prev = to_add def deleteAtIndex(self, index: int) -> None: """ Delete the index-th node in the linked list, if the index is valid. """ # if the index is invalid, do nothing if index < 0 or index >= self.size: return # find predecessor and successor of the node to be deleted if index < self.size - index: pred = self.head for _ in range(index): pred = pred.next succ = pred.next.next else: succ = self.tail for _ in range(self.size - index - 1): succ = succ.prev pred = succ.prev.prev # delete pred.next self.size -= 1 pred.next = succ succ.prev = pred
[solution2-Java]
public class ListNode { int val; ListNode next; ListNode prev; ListNode(int x) { val = x; } } class MyLinkedList { int size; // sentinel nodes as pseudo-head and pseudo-tail ListNode head, tail; public MyLinkedList() { size = 0; head = new ListNode(0); tail = new ListNode(0); head.next = tail; tail.prev = head; } /** 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; // choose the fastest way: to move from the head // or to move from the tail ListNode curr = head; if (index + 1 < size - index) for(int i = 0; i < index + 1; ++i) curr = curr.next; else { curr = tail; for(int i = 0; i < size - index; ++i) curr = curr.prev; } 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) { ListNode pred = head, succ = head.next; ++size; ListNode toAdd = new ListNode(val); toAdd.prev = pred; toAdd.next = succ; pred.next = toAdd; succ.prev = toAdd; } /** Append a node of value val to the last element of the linked list. */ public void addAtTail(int val) { ListNode succ = tail, pred = tail.prev; ++size; ListNode toAdd = new ListNode(val); toAdd.prev = pred; toAdd.next = succ; pred.next = toAdd; succ.prev = toAdd; } /** 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; // find predecessor and successor of the node to be added ListNode pred, succ; if (index < size - index) { pred = head; for(int i = 0; i < index; ++i) pred = pred.next; succ = pred.next; } else { succ = tail; for (int i = 0; i < size - index; ++i) succ = succ.prev; pred = succ.prev; } // insertion itself ++size; ListNode toAdd = new ListNode(val); toAdd.prev = pred; toAdd.next = succ; pred.next = toAdd; succ.prev = 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; // find predecessor and successor of the node to be deleted ListNode pred, succ; if (index < size - index) { pred = head; for(int i = 0; i < index; ++i) pred = pred.next; succ = pred.next.next; } else { succ = tail; for (int i = 0; i < size - index - 1; ++i) succ = succ.prev; pred = succ.prev.prev; } // delete pred.next --size; pred.next = succ; succ.prev = pred; } }

复杂度分析

  • 时间复杂度:
    • addAtHeadaddAtTail: $\mathcal{O}(1)$
    • getaddAtIndexdelete:$\mathcal{O}(\min(k, N - k))$,其中 $k$ 指的是元素的索引。
  • 空间复杂度:所有的操作都是 $\mathcal{O}(1)$。

统计信息

通过次数 提交次数 AC比率
72610 223878 32.4%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
819-最常见的单词(Most Common Word)
下一篇:
820-单词的压缩编码(Short Encoding of Words)
本文目录
本文目录