先决条件 –哈希介绍,使用单向链表的哈希表和在Java使用单独的链实现我们自己的哈希表
通过双向链表实现哈希表类似于使用单链表实现哈希表。唯一的区别是链表的每个节点都有下一个节点和上一个节点的地址。这将加快从列表中添加和删除元素的过程,因此时间复杂度将大大降低。
例子:
If we have a Singly linked list:
If we are at 3 and there is a need to remove it, then 2 need to be linked with 4 and as from 3, 2 can’t be accessed as it is singly linked list. So, the list has to be traversed again i.e O(n), but if we have doubly linked list i.e.
2 & 4 can be accessed from 3, hence in O(1), 3 can be removed.
下面是上述方法的实现:
1->2->3->4
输出:
1234
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。