Skip to content

Commit

Permalink
linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
brendonmiranda committed Sep 10, 2024
1 parent db80a52 commit 793afdd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package data.structure.linkedlist;
package cracking.the.code.interview.chapter2;

import java.util.HashSet;
import java.util.Set;

/**
* Write code to remove duplicates from an unsorted linked list.
*/
public class RemoveDup {
public class RemoveDupQuestion21 {

public static class Node {
public Node prev;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package data.structure;
package cracking.the.code.interview.chapter2;

import data.structure.linkedlist.RemoveDup;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.LinkedList;

public class RemoveDupTest {
public class RemoveDupQuestion21Test {

@Test
public void removeDupTest() {
Expand All @@ -18,7 +17,7 @@ public void removeDupTest() {
linkedList.add(6);
linkedList.add(2);

RemoveDup removeDup = new RemoveDup();
RemoveDupQuestion21 removeDup = new RemoveDupQuestion21();
var removedDuplicatedDoublyLinkedList =
removeDup.removeDup(makeDoublyCustomLinkedList(linkedList));

Expand Down Expand Up @@ -49,7 +48,7 @@ public void removeDupWithNoBufferTest() {
linkedList.add(6);
linkedList.add(2);

RemoveDup removeDup = new RemoveDup();
RemoveDupQuestion21 removeDup = new RemoveDupQuestion21();
var removedDuplicatedDoublyLinkedList =
removeDup.removeDupWithNoBuffer(makeDoublyCustomLinkedList(linkedList));

Expand All @@ -70,17 +69,17 @@ public void removeDupWithNoBufferTest() {
Assertions.assertNull(removedDuplicatedDoublyLinkedList.next);
}

public RemoveDup.Node makeDoublyCustomLinkedList(final LinkedList<Integer> linkedList) {
final RemoveDup.Node root = new RemoveDup.Node();
public RemoveDupQuestion21.Node makeDoublyCustomLinkedList(final LinkedList<Integer> linkedList) {
final RemoveDupQuestion21.Node root = new RemoveDupQuestion21.Node();

RemoveDup.Node child = root;
RemoveDupQuestion21.Node child = root;
for (int i = 0; i < linkedList.size(); i++) {
int value = linkedList.get(i);
child.value = value;
RemoveDup.Node prev = child;
RemoveDupQuestion21.Node prev = child;

if (i != linkedList.size() - 1) { // last loop
child.next = new RemoveDup.Node();
child.next = new RemoveDupQuestion21.Node();
child = child.next;
child.prev = prev;
}
Expand Down

0 comments on commit 793afdd

Please sign in to comment.