Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a program to remove duplicates from an unsorted singlely liked … #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
71 changes: 71 additions & 0 deletions JavaScript/chapter02/2.1 - Remove Dups/solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class Node {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i forgot to notice this before, but can you add your solution to a file called aditya9061.js?

constructor(data) {
this.data = data;
this.next = null;
}
}

class LinkedList {
constructor() {
this.head = null;
}

//Function to insert a node in the singlely linkedlist.
insert(data) {
var newNode = new Node(data);
if (this.head == null) {
this.head = newNode;
} else {
var currentNode = this.head;
while (currentNode.next) {
currentNode = currentNode.next;
}

currentNode.next = newNode;
}
}

//Function to print the singlely linkedlist.
printList() {
let currentNode = this.head;
let list = "";
while (currentNode) {
list += currentNode.data + "->";
currentNode = currentNode.next;
}
list += "null";
console.log(list);
}

//Function to remove dupliicate nodes from the singlely linkedlist.
removeDuplicates() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the runtime/space complexity of this function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The time complexity for removeDuplicates function is O(n²) and space complexity is O(1).
And time complexity for printList is O(n) and space complexity is O(1).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way to make it faster?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe by using recursion it can be made faster

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what would the space/time complexity be with recursion?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O(n)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you expand on that? usually with recursive algorithms, analysis becomes complicated because of the fact that computation across method calls need to be accounted for.

let currentNode1;
currentNode1 = this.head;
while (currentNode1 && currentNode1.next) {
let currentNode2 = currentNode1;
while (currentNode2.next) {
if (currentNode1.data == currentNode2.next.data) {
currentNode2.next = currentNode2.next.next;
} else {
currentNode2 = currentNode2.next;
}
}
currentNode1 = currentNode1.next;
}
}
}
//main function.
lhchavez marked this conversation as resolved.
Show resolved Hide resolved
var list1 = new LinkedList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm trying to make it easier to know if your code is correct, so I added support for mocha in #47

Can you change the code so that it follows the pattern found in https://github.com/techqueria/data-structures-and-algorithms/blob/aa70628efaf99a825115cea3c1008a4ef4276816/JavaScript/chapter01/1.1%20-%20Is%20Unique/solution.js ? you might need to change printList() in favor of toString() so that you can use assert.equal(list.toString(), '...');

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I will make the changes soon

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that change has been merged now!

list1.insert(0);
list1.insert(1);
list1.insert(2);
list1.insert(3);
list1.insert(1);
list1.insert(4);
list1.insert(4);
console.log("Original List with Duplicates:");
list1.printList();
list1.removeDuplicates();
console.log("********************************");
console.log("New List free from Duplicates:");
list1.printList();