-
Notifications
You must be signed in to change notification settings - Fork 95
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class Node { | ||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the runtime/space complexity of this function? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a way to make it faster? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe by using recursion it can be made faster There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what would the space/time complexity be with recursion? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. O(n) There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure I will make the changes soon There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); |
There was a problem hiding this comment.
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
?