Skip to content

Commit 713e217

Browse files
committed
Linked List README file added
1 parent 08688a0 commit 713e217

File tree

11 files changed

+158
-0
lines changed

11 files changed

+158
-0
lines changed

Data Structure/Linked List/README.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# Linked List
2+
3+
A **linked list** is a linear data structure where each element is a separate object.
4+
Each element (known as *node*) of a list is comprising of two items:
5+
- the data and
6+
- a reference to the next node.
7+
8+
The last node has a reference to `null`. The entry point into a linked list is called the `head` of the list. It should be noted that head is not a separate node, but the reference to the first node. If the list is empty then the head is a null reference.
9+
10+
![Linked List](./images/linked-list.png)
11+
12+
A linked list is a dynamic data structure. The number of nodes in a list is not fixed and can grow and shrink on demand. Any application which has to deal with an unknown number of objects can use a linked list.
13+
14+
15+
## Basic Operations:
16+
17+
Following are the basic operations supported by a list.
18+
19+
### Insertion
20+
21+
In order to add data to the list, we would need to create a new node (to store the new piece of data), and then manipulate the links so that the chain would also include this new node.
22+
23+
In a single linked list, the insertion operation can be performed in three ways.
24+
25+
* **Inserting At Beginning of the list**
26+
* **Inserting At End of the list**
27+
* **Inserting At Specific location in the list**
28+
29+
#### Inserting At Beginning of the list
30+
31+
![Insertion at the front](./images/insertion-front.png)
32+
33+
34+
* Create a new NODE with given value.
35+
* Set the new NODE's next reference, to what the HEAD is pointing to
36+
* Set the HEAD reference to the new NODE
37+
38+
39+
#### Inserting At End of the list
40+
41+
![Insertion at the end](./images/insertion-end.png)
42+
43+
* Seek through the list until the final NODE is reached
44+
* Create a new NODE, using the data to be inserted
45+
* Set the new NODE's next reference to null
46+
* Set the last NODE's next reference to the new NODE
47+
48+
49+
#### Inserting to the Middle
50+
51+
![Insertion at the middle](./images/insertion-middle.png)
52+
53+
* Seek through the list, until the NODE before the desired insertion point is found.
54+
* Create the new NODE, using the data to be inserted
55+
* Set the new NODE's next reference to the same as the next reference of the NODE before the insertion point
56+
* Set the NODE before the insertion point's next reference to the new NODE
57+
58+
59+
### Deletion
60+
61+
Searching and deletion operations are related by the fact that in order to delete an item from the list, it must first be found. Deletion of nodes relies on manipulating the links between data items so one is 'left out' of the chain - so it is then deleted by the garbage collector or the memory can be freed by the code itself.
62+
63+
In a single linked list, the deletion operation can be performed in three ways.
64+
65+
* **Deleting from Beginning of the list**
66+
* **Deleting from End of the list**
67+
* **Deleting a specific node**
68+
69+
#### Deleting from Beginning of the list
70+
71+
![Deletion from the front](./images/deletion-front.png)
72+
73+
74+
* If the list is not empty, just referencing the current HEAD to the next Node will remove the front Node
75+
76+
77+
#### Deleting from End of the list
78+
79+
![Deletion from the end](./images/deletion-last.png)
80+
81+
* Seek through the list until the previous node to the final NODE is reached
82+
* Reference this previous node to null and The target node is then no longer referred to by any variables and will be considered as deleted.
83+
84+
85+
#### Deleting a specific node
86+
87+
![Deletion at the middle](./images/deletion-middle.png)
88+
89+
* Search the data item to delete.
90+
* If found, go to the node immediately preceding the target. This cannot be done by simply following links; the search will have to hold the 'previous' node, as well as the 'currently testing' node during the search operation.
91+
* Set the previous node's next reference to point to the same node as the target node's next reference. The target node is then no longer referred to by any variables, and is deleted by the garbage collector.
92+
93+
94+
### Searching
95+
96+
In order to search through the list (to find a piece of data, or an insertion point for some new data), the only option is to traverse through the data one by one, from the start. This is known as a linear search. More efficient search techniques (such as the binary search) cannot be performed, as the link structure between data forces sequential access.
97+
98+
![Searching](./images/search.gif)
99+
100+
The step-by-step algorithm to search is, starting at the first data node, and comparing the search key with the corresponding data in the node:
101+
102+
* if the data matches, the search is complete.
103+
* if there is no match, move to the next node and repeat;
104+
* If the next reference is null, the end of the list has been reached; therefore, the data does not exist in the list. The algorithm can now terminate.
105+
106+
107+
108+
### Complexity Analysis
109+
110+
- Indexing - *O(n)*
111+
- Search - *O(n)*
112+
- Insertion
113+
- at beginning - *O(1)*
114+
- at end - *O(n)*
115+
- in middle - *Searching O(n) + insertion O(1)*
116+
- Deletion
117+
- at beginning- *O(1)*
118+
- at end - *O(n)*
119+
- in middle - *Searching O(n) and deletion O(1)*
120+
- Space - O(n)
121+
122+
123+
124+
### Variations
125+
126+
#### Doubly Linked List
127+
128+
In a single linked list, every node has link to its next node in the sequence. So, we can traverse from one node to other node only in one direction and we can not traverse back.
129+
130+
Double linked list is a sequence of elements in which every element has links to its previous element and next element in the sequence.
131+
132+
![Doubly Linked List](./images/doubly.png)
133+
134+
In double linked list, every node has link to its previous node and next node. So, we can traverse forward by using next field and can traverse backward by using previous field.
135+
136+
Doubly Linked List Implementation is almost same as the Singly Linked List except the places where we can use the facility of the Double Pointers (`next` and `prev`). In every operations we need to be careful about updating both of them.
137+
138+
[More about Doubly Linked List Operations](http://btechsmartclass.com/DS/U1_T12.html)
139+
140+
141+
#### Circular Linked List
142+
143+
In single linked list, every node points to its next node in the sequence and the last node points NULL. But in circular linked list, every node points to its next node in the sequence but the last node points to the first node in the list. That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.
144+
145+
![Circular Linked List](./images/circular.png)
146+
147+
Circular lists are useful in applications to repeatedly go around the list (implementing round-robin algorithms). For example, when multiple applications are running on a PC, it is common for the operating system to put the running applications on a list and then to cycle through them, giving each of them a slice of time to execute, and then making them wait while the CPU is given to another application. It is convenient for the operating system to use a circular list so that when it reaches the end of the list it can cycle around to the front of the list.
148+
149+
[More about Circular Linked List Operations](http://btechsmartclass.com/DS/U1_T11.html)
150+
151+
152+
#### Array vs Linked List
153+
http://www.geeksforgeeks.org/linked-list-vs-array/
154+
155+
### More on this topic
156+
- https://en.wikipedia.org/wiki/Linked_list
157+
- https://www.hackerearth.com/practice/data-structures/linked-list/singly-linked-list/tutorial/
158+
- https://www.tutorialspoint.com/data_structures_algorithms/linked_list_algorithms.htm
4.39 KB
Loading
Loading
Loading
Loading
7.34 KB
Loading
Loading
Loading
Loading
Loading
4.49 KB
Loading

0 commit comments

Comments
 (0)