Skip to content

Commit

Permalink
singly & doubly linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Oct 31, 2024
1 parent d1360fc commit 0ca45c5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cracking.the.code.interview.chapter4;

public class DoublyLinkedList {


public static void main (String... args) {

Node n = new Node();
insert(n,1);
insert(n,2);
insert(n,3);
insert(n,4);
insert(n,5);

}

private static class Node {
Node next;
Node previous;
Integer value;
}

private static Node insert(Node node, int v) {

if (node.value == null) {
node.value = v;
return node;
}

if (node.next == null) {
node.next = new Node();
node.next.previous = node;
}

node.next = insert(node.next, v);

return node;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cracking.the.code.interview.chapter4;

public class SinglyLinkedList {


public static void main(String... args) {

Node root = new Node();

insert(root, 1);
insert(root, 2);
insert(root, 3);
insert(root, 4);
insert(root, 5);


}

private static class Node {
private Node next;
private Integer value;
}

private static Node insert(Node node, int v) {
var child = node;
if(child.value == null) {
child.value = v;
return child;
}

if(child.next == null) {
child.next = new Node();
}

child.next = insert(child.next, v);

return node;
}

}

0 comments on commit 0ca45c5

Please sign in to comment.