public class LinkedList {
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
private Node head;
LinkedList() {
this.head = null;
}
LinkedList(int data) {
this.head = new Node(data);
}
public void insertFront(int data) {
if(this.head == null) this.head = new Node(data);
else {
Node temp = new Node(data);
temp.next = this.head;
this.head = temp;
}
}
public void insertBack(int data) {
if(this.head == null) this.head = new Node(data);
else {
Node temp = this.head;
while(temp.next != null) temp = temp.next;
temp.next = new Node(data);
}
}
public int removeFront() {
int data = this.head.data;
Node temp = this.head;
this.head = temp.next;
temp = null;
return data;
}
public int removeBack() {
Node temp = this.head;
while(temp.next.next != null) temp = temp.next;
int data = temp.next.data;
temp.next = null;
return data;
}
public void show() {
Node temp = this.head;
while (temp != null) {
System.out.printf("%d ", temp.data);
temp = temp.next;
}
System.out.println();
}
}
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)