|
1 | 1 | package src.dataStructures.linkedList;
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - * There are many variations when it comes to implementing linked lists. Here's mine. |
5 |
| - * A linked list that tracks both head and tail may improve the complexity of some of the methods below |
6 |
| - * (e.g being able to branch off and create a new Linked List in O(1) rather than iterating to find size of branched off list) |
7 |
| - * |
8 |
| - * Constructors: |
9 |
| - * LinkedList() -- Initialise a link list with a null head; size = 0 |
10 |
| - * LinkedList(Node<T> head) -- given a node, make this node the head of a new linked list; iterate to find the size |
| 4 | + * <p></p> |
| 5 | + * There are many variations when it comes to implementing linked lists. Here's ours. <br> |
| 6 | + * A linked list that tracks both head and tail may improve the complexity of some methods below <br> |
| 7 | + * (e.g being able to branch off and create a new Linked List in O(1) |
| 8 | + * rather than iterating to find size of branched off list) |
| 9 | + * <p></p> |
| 10 | + * Constructors: <br> |
| 11 | + * LinkedList() -- Initialise a link list with a null head; size = 0 <br> |
| 12 | + * LinkedList(Node<T> head) -- given a node, make node the head of a new linked list; |
| 13 | + * size is found by iterating to the end from this specified head <br> |
11 | 14 | * LinkedList(Node<T>head, int size) -- given a head node and size of linked list specified;
|
12 | 15 | * made private to avoid client from constructing a linked list with invalid size
|
13 |
| - * |
14 |
| - * Callable methods are: |
15 |
| - * size() -- Gets the size of the linked list |
16 |
| - * insertFront(T object) -- inserts the object at the front of the linked list |
17 |
| - * insertEnd(T object) -- inserts the object at the end of the linked list |
18 |
| - * insert(T object, int idx) -- inserts the object at the specified index of the linked list |
19 |
| - * remove(int idx) -- remove the node at the specified index |
20 |
| - * delete(T object) -- delete the 1st encounter of the specified object from the linked list |
21 |
| - * pop() -- remove the last node from the linked list |
22 |
| - * poll() -- remove the first node from the linked list |
23 |
| - * search(T object) -- search for the 1st encounter of the node that holds the specified object |
24 |
| - * get(int idx) -- get the node at the specified index |
25 |
| - * reverse() -- reverse the linked list (head of linked list now starts from the back) |
26 |
| - * sort() -- sorts the linked list by their natural order |
27 |
| - * |
| 16 | + * <p></p> |
| 17 | + * Callable methods are: <br> |
| 18 | + * size() -- Gets the size of the linked list <br> |
| 19 | + * insertFront(T object) -- inserts the object at the front of the linked list <br> |
| 20 | + * insertEnd(T object) -- inserts the object at the end of the linked list <br> |
| 21 | + * insert(T object, int idx) -- inserts the object at the specified index of the linked list <br> |
| 22 | + * remove(int idx) -- remove the node at the specified index <br> |
| 23 | + * delete(T object) -- delete the 1st encounter of the specified object from the linked list <br> |
| 24 | + * pop() -- remove the last node from the linked list <br> |
| 25 | + * poll() -- remove the first node from the linked list <br> |
| 26 | + * search(T object) -- search for the 1st encounter of the node that holds the specified object <br> |
| 27 | + * get(int idx) -- get the node at the specified index <br> |
| 28 | + * reverse() -- reverse the linked list (head of linked list now starts from the back) <br> |
| 29 | + * sort() -- sorts the linked list by their natural order <br> |
| 30 | + * |
28 | 31 | * @param <T> generic type for objects to be stored in the linked list
|
29 | 32 | */
|
30 | 33 | public class LinkedList<T extends Comparable<T>> {
|
@@ -241,9 +244,9 @@ public void reverse() {
|
241 | 244 | /**
|
242 | 245 | * Sorts the linked list by the natural order of the elements.
|
243 | 246 | * Generally, merge sort is the most efficient sorting algorithm for linked lists.
|
244 |
| - * Addressing a random node in the linked list incurrs O(n) time complexity. |
| 247 | + * Accessing a random node in the linked list incurs O(n) time complexity. |
245 | 248 | * This makes sort algorithms like quicksort and heapsort inefficient since they rely
|
246 |
| - * on the O(1) lookup time of an array. |
| 249 | + * on the O(1) lookup time, like in an array. |
247 | 250 | *
|
248 | 251 | * A good video to visualise this algorithm can be found
|
249 | 252 | * <a = https://www.youtube.com/watch?v=JSceec-wEyw, href = "url">here</a>.
|
|
0 commit comments