-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.java
More file actions
152 lines (114 loc) · 2.97 KB
/
LinkedList.java
File metadata and controls
152 lines (114 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package datastructure.linkedlist;
public class LinkedList<E extends Comparable<E>> {
private Node<E> head;
public LinkedList() {
head = null;
}
public void addFirst(E data) {
head = new Node<E>(data, head);
}
// Load item into linked in sorted order
public void addInPlace(E data) {
Node<E> curr, prev, np;
prev = null;
np = new Node<E>(data, null);
curr = head;
while (curr != null && data.compareTo(curr.data) > 0) {
prev = curr;
curr = curr.next;
} // end of while
np.next = curr;
if (prev == null)
head = np; // this node is the new head
else
prev.next = np; // prev node now re-points to new node
} // addInPlace()
public void addLast(E data) {
if (head == null)
addFirst(data);
else {
Node<E> temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = new Node<E>(data, null);
} // end of else
} // end of addLast()
public E removeFirst() throws NoSuchElementException {
Node<E> temp;
if (head == null)
throw new NoSuchElementException();
else {
temp = head;
head = head.next;
} // end of else
return temp.data;
} // end of removeFirst()
public E removeLast() throws NoSuchElementException {
Node<E> temp;
Node<E> prev = new Node<E>();
E data;
if (head == null)
throw new NoSuchElementException();
else {
temp = head;
while (temp.next != null) {
prev = temp;
temp = temp.next;
} // end of while
data = prev.data;
prev.next = null;
} // end of else
return data;
} // end of remove last
// Reverse a linked list
public LinkedList<E> reverseList() {
Node<E> temp = head;
LinkedList<E> rl = new LinkedList<E>();
while (temp.next != null) {
rl.addFirst(temp.data);
temp = temp.next;
}
rl.addFirst(temp.data);
return rl;
}
public void mergedLinkedLists(LinkedList<E> listOne, LinkedList<E> listTwo, LinkedList<E> mergedList) {
Node<E> listOneCurr = listOne.head;
Node<E> listTwoCurr = listTwo.head;
while (listOneCurr != null && listTwoCurr != null) {
if (listOneCurr.data.compareTo(listTwoCurr.data) > 0) {
mergedList.addLast(listTwoCurr.data);
listTwoCurr = listTwoCurr.next;
} // end of if
else {
mergedList.addLast(listOneCurr.data);
listOneCurr = listOneCurr.next;
}
} // end of while
// List one is finished. Copy over rest of the elements of list two
if (listOneCurr == null) {
while (listTwoCurr != null) {
mergedList.addLast(listTwoCurr.data);
listTwoCurr = listTwoCurr.next;
}
} // end of if
// List two is finished. Copy over rest of the elements of list one
else {
while (listOneCurr != null) {
mergedList.addLast(listOneCurr.data);
listOneCurr = listOneCurr.next;
}
} // end of else
} // end of mergedLinkedList()
public void printContent() {
Node<E> temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public Node<E> getHead() {
return head;
}
} // end of class