11import java .lang .reflect .Array ;
22
3- class LinkedListNode <T > {
4- public T data ;
5- protected LinkedListNode <T > next ;
6-
7- public LinkedListNode (T data , LinkedListNode <T > next ) {
8- this .data = data ;
9- this .next = next ;
10- }
11- }
12-
133/**
144 * Simple linked list implementation.
155 *
166 * <p>
177 * Operations with their complexities are:
188 * <ul>
19- * <li>Get data at an index: O(n)</li>
20- * <li>Insert data at head: O(1)</li>
21- * <li>Insert data at an index: O(n)</li>
22- * <li>Delete data at an index: O(n)</li>
9+ * <li><code>get</code>: O(n)</li>
10+ * <li><code>insert</code>: O(n)</li>
11+ * <li><code>insertAtHead</code>: O(1)</li>
12+ * <li><code>delete</code>: O(n)</li>
13+ * <li><code>deleteAtHead</code>: O(1)</li>
2314 * </ul>
2415 */
2516public class LinkedList <T > {
26- /**
27- * The head of the linked list which is a <code>LinkedListNode</code> that
28- * points to the first node of the linked list.
29- *
30- * <p>
31- * Having the head being an empty node rather than the first node in the
32- * actual list helps clean the code and reduces if statements.
33- */
3417 private LinkedListNode <T > head ;
35- /**
36- * The number of nodes in this linked list, excluding the head.
37- */
3818 private int length ;
3919
20+ @ SuppressWarnings ("unchecked" )
21+ public static <T > T [] toArray (LinkedListNode <T > head , int length , Class <T > c ) {
22+ T [] array = (T []) Array .newInstance (c , length );
23+ LinkedListNode <T > node = head ;
24+
25+ for (int i = 0 ; i < length ; i ++) {
26+ node = node .next ;
27+ array [i ] = node .data ;
28+ }
29+
30+ return array ;
31+ }
32+
4033 public LinkedList () {
4134 this .head = new LinkedListNode <T >(null , null );
4235 this .length = 0 ;
@@ -51,28 +44,17 @@ public LinkedList(T[] data) {
5144 }
5245
5346 public LinkedListNode <T > getHead () {
54- return this .head ;
47+ return this .head . next ;
5548 }
5649
5750 public int getLength () {
5851 return this .length ;
5952 }
6053
61- /**
62- * Checks if this list is empty.
63- *
64- * @return <code>true</code> if this list is empty, <code>false</code> if not.
65- */
6654 public boolean isEmpty () {
6755 return this .length == 0 ;
6856 }
6957
70- /**
71- * Returns the data contained by the node at the specified index.
72- *
73- * @param index
74- * @return The data stored at the index.
75- */
7658 public T get (int index ) {
7759 if (index < 0 || index >= this .length ) throw new IndexOutOfBoundsException ();
7860
@@ -84,35 +66,6 @@ public T get(int index) {
8466 return node .data ;
8567 }
8668
87- /**
88- * Inserts new data at the head.
89- *
90- * <p>
91- * O(1) complexity.
92- *
93- * @param data
94- * @return The new node inserted that contains the data.
95- */
96- public LinkedListNode <T > insertAtHead (T data ) {
97- LinkedListNode <T > newNode = new LinkedListNode <T >(data , this .head .next );
98- this .head .next = newNode ;
99-
100- this .length ++;
101- return newNode ;
102- }
103-
104- /**
105- * Inserts new data at a specified index, shifting existing node at the index
106- * and all nodes after it to the right.
107- *
108- * <p>
109- * O(n) complexity.
110- *
111- * @param data
112- * @param index A valid index for the new node that is between 0 and
113- * <code>this.length</code> (inclusive).
114- * @return The new node inserted that contains the data.
115- */
11669 public LinkedListNode <T > insert (T data , int index ) {
11770 if (index < 0 || index > this .length ) throw new IndexOutOfBoundsException ();
11871
@@ -129,15 +82,14 @@ public LinkedListNode<T> insert(T data, int index) {
12982 return newNode ;
13083 }
13184
132- /**
133- * Deletes the node at the specified index.
134- *
135- * <p>
136- * O(n) complexity.
137- *
138- * @param index
139- * @return The deleted node.
140- */
85+ public LinkedListNode <T > insertAtHead (T data ) {
86+ LinkedListNode <T > newNode = new LinkedListNode <T >(data , this .head .next );
87+ this .head .next = newNode ;
88+
89+ this .length ++;
90+ return newNode ;
91+ }
92+
14193 public LinkedListNode <T > delete (int index ) {
14294 if (index < 0 || index >= this .length ) throw new IndexOutOfBoundsException ();
14395
@@ -146,28 +98,22 @@ public LinkedListNode<T> delete(int index) {
14698 for (int i = 0 ; i < index ; i ++)
14799 prevNode = prevNode .next ;
148100
149- LinkedListNode <T > node = prevNode .next ;
150- if (index != this .length ) prevNode .next = node .next ;
101+ LinkedListNode <T > deletedNode = prevNode .next ;
102+ if (index != this .length ) prevNode .next = deletedNode .next ;
151103
152104 this .length --;
153- return node ;
105+ return deletedNode ;
154106 }
155107
156- /**
157- * Exports this list to an array.
158- *
159- * @return
160- */
161- @ SuppressWarnings ("unchecked" )
162- public T [] toArray (Class <T > c ) {
163- T [] array = (T []) Array .newInstance (c , this .length );
164- LinkedListNode <T > node = this .head ;
108+ public LinkedListNode <T > deleteAtHead () {
109+ if (this .head .next != null ) {
110+ LinkedListNode <T > deletedNode = this .head .next ;
111+ this .head .next = this .head .next .next ;
165112
166- for (int i = 0 ; i < this .length ; i ++) {
167- node = node .next ;
168- array [i ] = node .data ;
113+ this .length --;
114+ return deletedNode ;
115+ } else {
116+ return null ;
169117 }
170-
171- return array ;
172118 }
173119}
0 commit comments