From 0ea70e212255e351bd1f328326cffc37930f650c Mon Sep 17 00:00:00 2001
From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com>
Date: Wed, 2 Aug 2023 19:11:01 -0700
Subject: [PATCH] Update README.md
---
linked_lists/README.md | 69 ++++++++++++++++++++++++++++++++++++++++--
1 file changed, 66 insertions(+), 3 deletions(-)
diff --git a/linked_lists/README.md b/linked_lists/README.md
index 89ada1f..9f4f9e1 100644
--- a/linked_lists/README.md
+++ b/linked_lists/README.md
@@ -1,10 +1,12 @@
-## Linked List
+## Linked Lists
-* each node in a singly-linked list contains a valye and a reference fiedl to link to the next node. the head node (first node) usually represents the whole list.
+* each node in a singly-linked list contains a value and a reference field to link to the next node. the head node (first node) usually represents the whole list.
* unlike an array, a linked list does not provide constant time access to an index (as it needs to interact through all k elements), however addition and removal of elements are constant time.
+ * 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.
* nodes can be added at the beginning, head needs to be update (`current -> head` and `head = current`).
@@ -12,6 +14,12 @@
+
+
+
+
+
+
---
@@ -19,7 +27,9 @@
-* can be done with hash table or the two-pointer technique, where if there is no cycle, the faster pointer (going 2 steps) will stop at the end of the list, but if there is a cycle, the fast pointer will eventually meet the slow pointer (going 1 step).
+* cycles in a linked list can be detecting by using a hash table or with the two-pointer technique.
+
+* if there is no cycle, the faster pointer (going 2 steps) will stop at the end of the list, but if there is a cycle, the fast pointer will eventually meet the slow pointer (going 1 step).
* if there is no cycle, the faster pointer takes `N/2` to reach the end of the list (`N` being the length).
@@ -56,6 +66,59 @@ def reverse_list(head):
+---
+
+### removing linked list elements
+
+
+
+* given a head of a linked list and a value, how to remove all the nodes of the list that have that value?
+
+* this problem is easy if one has to delete a node in the middle, as all you need to do is loop until the predecessor node and change the pointers.
+
+* however, if the node to be deleted is in the head of the list, the best way is to use a sentinel node. sentinel nodes are widely used in trees and linked lists as pseudo-heads, pseudo-tails, markers of level end, etc. they are purely functional and usually does not hold any data. their main purpose is to standardize the process (by making the list never be empty or headless).
+
+* examples are LRU cache (where sentinel nodes are used as pseudo-head and pseudo-tail) and tree level order traversal (where sentinel nodes are used to mark level end).
+
+
+
+```python
+def remove_elements(head, val):
+
+ sentinel = ListNode(0)
+ sentinel.next = head
+ prev, current = sentinel, head
+
+ while current:
+ if current.val == val:
+ prev.next = current.next
+ else:
+ prev = current
+ current = current.next
+
+ return sentinel.next
+```
+
+
+
+----
+
+### doubly linked lists
+
+
+
+* in doubly linked lists, a node has a `previous` field.
+
+* when it comes to add and delete operations, extra attention is needed when you want to delete or insert at the beginning or at the end of the list.
+
+
+
+```python
+
+
+```
+
+
----