-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
175 lines (135 loc) · 4.07 KB
/
LinkedList.java
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
public class LinkedList<T> {
Node head; // Head of list
class Node {
T data;
Node next;
// Constructors
Node(T d) {
data = d;
next = null;
}
}
public LinkedList() { // Linked List HeaderNode
head = new Node(null);
}
public LinkedList(T data) { // Linked List HeaderNode
head = new Node(data);
}
// Defining the method to Insert at front => O(1)
public void insertFront(T data) {
Node newNode = new Node(data);
newNode.next = this.head.next;
this.head.next = newNode;
}
public void insertEnd(T data) {
Node newNode = new Node(data);
// If LL is empty then make the new node as head
if (this.head.data == null) {
this.head = newNode;
} else {
// Else traverse till last node & insert the new node
Node currNode = this.head;
while (currNode.next != null) {
currNode = currNode.next;
}
currNode.next = newNode;
}
}
public void insertKey(T data, T key) {
Node newNode = new Node(data);
newNode.next = null;
Node temp = this.head;
boolean status = false;
while (temp != null) {
if (temp.data == key) {
status = true;
break;
}
temp = temp.next;
}
if (status) {
newNode.next = temp.next;
temp.next = newNode;
}
}
// Removes the value in the list
public void deleteKey(T key) {
Node currNode = this.head;
Node prev = null;
if (currNode != null && currNode.data == key) {
this.head = currNode.next;
System.out.println(key + " found and deleted");
}
while (currNode != null && currNode.data != key) {
prev = currNode;
currNode = currNode.next;
}
if (currNode != null) {
prev.next = currNode.next;
System.out.println(key + " found and deleted");
}
if (currNode == null) {
System.out.println(key + " not found");
}
}
// Removes the element at certain index
public void pop(int index) {
Node currNode = this.head, prev = null;
if (index == 1 && currNode != null) {
this.head = currNode.next;
System.out.println(index + " position element deleted");
}
int counter = 0;
while (currNode != null) {
if (counter == index) {
prev.next = currNode.next;
System.out.println(index + " position element deleted");
break;
} else {
prev = currNode;
currNode = currNode.next;
counter++;
}
}
if (currNode == null) {
System.out.println(index + " position element not found");
}
}
public void concatenate(LinkedList<T> l2) {
Node l1Node = this.head;
Node l2Node = l2.head;
while (l1Node.next != null) {
l1Node = l1Node.next;
}
l1Node.next = l2Node;
// free(l2.head);
}
public LinkedList<T> merge(LinkedList<T> l2) {
Node l1Node = this.head;
Node l2Node = l2.head;
LinkedList l3 = new LinkedList();
while (l1Node != null && l2Node != null) {
l3.insertEnd(l1Node.data);
l3.insertEnd(l2Node.data);
l1Node = l1Node.next;
l2Node = l2Node.next;
}
while (l1Node != null) {
l3.insertEnd(l1Node.data);
l1Node = l1Node.next;
}
while (l2Node != null) {
l3.insertEnd(l2Node.data);
l2Node = l2Node.next;
}
return l3;
}
public void printList() {
Node currNode = this.head;
while (currNode != null) {
System.out.print(currNode.data + " ");
currNode = currNode.next;
}
System.out.println();
}
}