66 * <p>
77 * Operations with their complexities are:
88 * <ul>
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>
9+ * <li><code>get(int) </code>: O(n)</li>
10+ * <li><code>insert(T, int) </code>: O(n)</li>
11+ * <li><code>insertAtHead(T) </code>: O(1)</li>
12+ * <li><code>delete(int) </code>: O(n)</li>
13+ * <li><code>deleteAtHead() </code>: O(1)</li>
1414 * </ul>
1515 */
1616public class LinkedList <T > {
17- private LinkedListNode <T > head ;
18- private int length ;
17+ LinkedNode <T > head ;
18+ int length ;
1919
2020 @ SuppressWarnings ("unchecked" )
21- public static <T > T [] toArray (LinkedListNode <T > head , int length , Class <T > c ) {
21+ public static <T > T [] toArray (LinkedNode <T > head , int length , Class <T > c ) {
2222 T [] array = (T []) Array .newInstance (c , length );
23- LinkedListNode <T > node = head ;
23+ LinkedNode <T > node = head ;
2424
2525 for (int i = 0 ; i < length ; i ++) {
2626 node = node .next ;
@@ -31,19 +31,11 @@ public static <T> T[] toArray(LinkedListNode<T> head, int length, Class<T> c) {
3131 }
3232
3333 public LinkedList () {
34- this .head = new LinkedListNode <T >(null , null );
34+ this .head = new LinkedNode <T >(null , null );
3535 this .length = 0 ;
3636 }
3737
38- public LinkedList (T [] data ) {
39- this .head = new LinkedListNode <T >(null , null );
40- this .length = 0 ;
41-
42- for (T d : data )
43- this .insert (d , this .length );
44- }
45-
46- public LinkedListNode <T > getHead () {
38+ public LinkedNode <T > getHead () {
4739 return this .head .next ;
4840 }
4941
@@ -56,21 +48,22 @@ public boolean isEmpty() {
5648 }
5749
5850 public T get (int index ) {
59- if (index < 0 || index >= this .length ) throw new IndexOutOfBoundsException ();
60-
61- LinkedListNode <T > node = this .head .next ;
51+ if (index < 0 || index >= this .length )
52+ throw new IndexOutOfBoundsException ();
6253
54+ LinkedNode <T > node = this .head .next ;
6355 for (int i = 0 ; i < index ; i ++)
6456 node = node .next ;
6557
6658 return node .data ;
6759 }
6860
69- public LinkedListNode <T > insert (T data , int index ) {
70- if (index < 0 || index > this .length ) throw new IndexOutOfBoundsException ();
61+ public LinkedNode <T > insert (T data , int index ) {
62+ if (index < 0 || index > this .length )
63+ throw new IndexOutOfBoundsException ();
7164
72- LinkedListNode <T > newNode = new LinkedListNode <T >(data , null );
73- LinkedListNode <T > prevNode = this .head ;
65+ LinkedNode <T > newNode = new LinkedNode <T >(data , null );
66+ LinkedNode <T > prevNode = this .head ;
7467
7568 for (int i = 0 ; i < index ; i ++)
7669 prevNode = prevNode .next ;
@@ -82,32 +75,33 @@ public LinkedListNode<T> insert(T data, int index) {
8275 return newNode ;
8376 }
8477
85- public LinkedListNode <T > insertAtHead (T data ) {
86- LinkedListNode <T > newNode = new LinkedListNode <T >(data , this .head .next );
78+ public LinkedNode <T > insertAtHead (T data ) {
79+ LinkedNode <T > newNode = new LinkedNode <T >(data , this .head .next );
8780 this .head .next = newNode ;
8881
8982 this .length ++;
9083 return newNode ;
9184 }
9285
93- public LinkedListNode <T > delete (int index ) {
94- if (index < 0 || index >= this .length ) throw new IndexOutOfBoundsException ();
86+ public LinkedNode <T > delete (int index ) {
87+ if (index < 0 || index >= this .length )
88+ throw new IndexOutOfBoundsException ();
9589
96- LinkedListNode <T > prevNode = this .head ;
90+ LinkedNode <T > prevNode = this .head ;
9791
9892 for (int i = 0 ; i < index ; i ++)
9993 prevNode = prevNode .next ;
10094
101- LinkedListNode <T > deletedNode = prevNode .next ;
95+ LinkedNode <T > deletedNode = prevNode .next ;
10296 if (index != this .length ) prevNode .next = deletedNode .next ;
10397
10498 this .length --;
10599 return deletedNode ;
106100 }
107101
108- public LinkedListNode <T > deleteAtHead () {
102+ public LinkedNode <T > deleteAtHead () {
109103 if (this .head .next != null ) {
110- LinkedListNode <T > deletedNode = this .head .next ;
104+ LinkedNode <T > deletedNode = this .head .next ;
111105 this .head .next = this .head .next .next ;
112106
113107 this .length --;
0 commit comments