Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2109,8 +2109,8 @@ In order to achieve greater coverage and encourage more people to contribute to
</a>
</td>
<td> <!-- Java -->
<a href="./CONTRIBUTING.md">
<img align="center" height="25" src="./logos/github.svg" />
<a href="./src/java/SinglyLinkedList.java">
<img align="center" height="25" src="./logos/java.svg" />
</a>
</td>
<td> <!-- Python -->
Expand Down
159 changes: 159 additions & 0 deletions src/java/SinglyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import java.util.Objects;

class Node {
int val;
Node next;

Node(int val) {
this.val = val;
this.next = null;
}

Node(int val, Node next) {
this.val = val;
this.next = next;
}
}

public class SinglyLinkedList {
Node head;

public void printList() {
Node aux = head;
System.out.print("[");
while (aux != null) {
System.out.print(aux.val);
if (aux.next != null) {
System.out.print(", ");
}
aux = aux.next;
}
System.out.println("]");
}

public void pushBack(int val) {
Node newNode = new Node(val);
if (head == null) {
head = newNode;
} else {
Node aux = head;
while (aux.next != null) {
aux = aux.next;
}
aux.next = newNode;
}
}

public void pushFront(int val) {
Node newNode = new Node(val, head);
head = newNode;
}

public void insertInPosition(int val, int pos) {
if (pos == 0) {
pushFront(val);
return;
}
Node newNode = new Node(val);
Node prev = head;
Node curr = head;

while (curr != null && pos > 0) {
prev = curr;
curr = curr.next;
pos--;
}

prev.next = newNode;
newNode.next = curr;
}

public void popBack() {
if (head == null) {
return;
}

if (head.next == null) {
head = null;
return;
}

Node prev = head;
Node curr = head.next;

while (curr.next != null) {
prev = prev.next;
curr = curr.next;
}

prev.next = null;
}

public void popFront() {
if (head == null) {
return;
}

Node aux = head;
head = aux.next;
}

public void removeFromPosition(int pos) {
if (head == null) {
return;
}

if (pos == 0) {
popFront();
return;
}

Node prev = head;
Node curr = head;

while (curr != null && pos > 0) {
prev = curr;
curr = curr.next;
pos--;
}

if (curr != null) {
prev.next = curr.next;
}
}

public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();

System.out.println("Inserting the elements at the end [1, 2, 3]:");
list.pushBack(1);
list.pushBack(2);
list.pushBack(3);
list.printList();

System.out.println("Inserting the elements at the beginning [0, -1, -2]:");
list.pushFront(0);
list.pushFront(-1);
list.pushFront(-2);
list.printList();

System.out.println("Inserting in positions 3, 4, 5 the elements [997, 998, 999] respectively:");
list.insertInPosition(997, 3);
list.insertInPosition(998, 4);
list.insertInPosition(999, 5);
list.printList();

System.out.println("Removing last element:");
list.popBack();
list.printList();

System.out.println("Removing first element:");
list.popFront();
list.printList();

System.out.println("Removing element in position 2:");
list.removeFromPosition(2);
list.printList();
}
}

Loading