From f18575e9afd4f40f9beccb3b6383de38aa0c8785 Mon Sep 17 00:00:00 2001 From: aditya9061 Date: Fri, 11 Oct 2019 21:40:26 +0530 Subject: [PATCH 1/3] Added a program to remove duplicates from an unsorted singlely liked list. --- .../chapter02/2.1 - Remove Dups/solution.js | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/JavaScript/chapter02/2.1 - Remove Dups/solution.js b/JavaScript/chapter02/2.1 - Remove Dups/solution.js index e69de29..6b732d6 100644 --- a/JavaScript/chapter02/2.1 - Remove Dups/solution.js +++ b/JavaScript/chapter02/2.1 - Remove Dups/solution.js @@ -0,0 +1,93 @@ +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(head===null){ + this.head=newnode; + } + + else{ + + var temp=this.head; + while(temp.next!==null){ + temp=temp.next; + } + + temp=newnode; + } + } + +//Function to print the singlely linkedlist. + printList=()=>{ + var temp=this.head; + var list; + while(temp!==null){ + list+=temp.data+"->"; + temp=temp.next; + } + list+="null"; + console.log(list); + } + + + +//Function to remove dupliicate nodes from the singlely linkedlist. + removeDuplicates=()=>{ + + var temp1,temp2,duplicate; + + temp1=this.head; + temp2=temp1; + while(temp1!==null || temp.next!==null){ + + while(temp2.next!==null){ + if(temp1.data===temp2.next.data){ + duplicate=temp2.next; + temp2.next=temp2.next.next; + delete(duplicate); + } + else{ + temp2=temp2.next; + } + } + temp1=temp1.next; + } + + } + +} + +//main function. +/* +Linkedlist list; +list.insert(0); +list.insert(1); +list.insert(2); +list.insert(3); +list.insert(1); +list.insert(4); +list.insert(4); +console.log("Original List with DUplicates:"); +list.printList(); +list.removeDuplicates(); +console.log("List free of duplicates:"); +list.printList(); +*/ + From e0006925c313e93e3bce23f9e0da6e234bedfb30 Mon Sep 17 00:00:00 2001 From: aditya9061 Date: Fri, 11 Oct 2019 23:29:52 +0530 Subject: [PATCH 2/3] Rectified some errors. --- .../chapter02/2.1 - Remove Dups/solution.js | 58 +++++++++---------- 1 file changed, 26 insertions(+), 32 deletions(-) diff --git a/JavaScript/chapter02/2.1 - Remove Dups/solution.js b/JavaScript/chapter02/2.1 - Remove Dups/solution.js index 6b732d6..1f16f96 100644 --- a/JavaScript/chapter02/2.1 - Remove Dups/solution.js +++ b/JavaScript/chapter02/2.1 - Remove Dups/solution.js @@ -16,29 +16,29 @@ class LinkedList{ } //Function to insert a node in the singlely linkedlist. - insert=(data)=>{ + insert(data){ var newnode=new node(data); - if(head===null){ + if(this.head==null){ this.head=newnode; } else{ var temp=this.head; - while(temp.next!==null){ + while(temp.next){ temp=temp.next; } - temp=newnode; + temp.next=newnode; } } //Function to print the singlely linkedlist. - printList=()=>{ + printList(){ var temp=this.head; - var list; - while(temp!==null){ + var list=""; + while(temp){ list+=temp.data+"->"; temp=temp.next; } @@ -49,19 +49,16 @@ class LinkedList{ //Function to remove dupliicate nodes from the singlely linkedlist. - removeDuplicates=()=>{ + removeDuplicates(){ - var temp1,temp2,duplicate; + var temp1,temp2; temp1=this.head; - temp2=temp1; - while(temp1!==null || temp.next!==null){ - - while(temp2.next!==null){ - if(temp1.data===temp2.next.data){ - duplicate=temp2.next; + while(temp1 && temp1.next){ + temp2=temp1; + while(temp2.next){ + if(temp1.data==temp2.next.data){ temp2.next=temp2.next.next; - delete(duplicate); } else{ temp2=temp2.next; @@ -73,21 +70,18 @@ class LinkedList{ } } - //main function. -/* -Linkedlist list; -list.insert(0); -list.insert(1); -list.insert(2); -list.insert(3); -list.insert(1); -list.insert(4); -list.insert(4); -console.log("Original List with DUplicates:"); -list.printList(); -list.removeDuplicates(); -console.log("List free of duplicates:"); -list.printList(); +/*var list_1=new LinkedList(); +list_1.insert(0); +list_1.insert(1); +list_1.insert(2); +list_1.insert(3); +list_1.insert(1); +list_1.insert(4); +list_1.insert(4); +console.log("Original List with Duplicates:"); +list_1.printList(); +list_1.removeDuplicates(); +console.log("New List free of duplicates:"); +list_1.printList(); */ - From fe06b171231ac75cdf3443ca13068b23a09dc48b Mon Sep 17 00:00:00 2001 From: aditya9061 Date: Sat, 12 Oct 2019 00:09:56 +0530 Subject: [PATCH 3/3] Tried to implement all the suggestions. --- .../chapter02/2.1 - Remove Dups/solution.js | 140 ++++++++---------- 1 file changed, 62 insertions(+), 78 deletions(-) diff --git a/JavaScript/chapter02/2.1 - Remove Dups/solution.js b/JavaScript/chapter02/2.1 - Remove Dups/solution.js index 1f16f96..8c0b05b 100644 --- a/JavaScript/chapter02/2.1 - Remove Dups/solution.js +++ b/JavaScript/chapter02/2.1 - Remove Dups/solution.js @@ -1,87 +1,71 @@ -class node{ - - constructor(data){ - - this.data=data; - this.next=null; - - } +class Node { + constructor(data) { + this.data = data; + this.next = null; + } } +class LinkedList { + constructor() { + this.head = 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 temp=this.head; - while(temp.next){ - temp=temp.next; - } - - temp.next=newnode; - } - } - -//Function to print the singlely linkedlist. - printList(){ - var temp=this.head; - var list=""; - while(temp){ - list+=temp.data+"->"; - temp=temp.next; - } - list+="null"; - console.log(list); - } + //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 remove dupliicate nodes from the singlely linkedlist. - removeDuplicates(){ - - var temp1,temp2; - - temp1=this.head; - while(temp1 && temp1.next){ - temp2=temp1; - while(temp2.next){ - if(temp1.data==temp2.next.data){ - temp2.next=temp2.next.next; - } - else{ - temp2=temp2.next; - } - } - temp1=temp1.next; - } - - } + //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() { + 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. -/*var list_1=new LinkedList(); -list_1.insert(0); -list_1.insert(1); -list_1.insert(2); -list_1.insert(3); -list_1.insert(1); -list_1.insert(4); -list_1.insert(4); +var list1 = new LinkedList(); +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:"); -list_1.printList(); -list_1.removeDuplicates(); -console.log("New List free of duplicates:"); -list_1.printList(); -*/ +list1.printList(); +list1.removeDuplicates(); +console.log("********************************"); +console.log("New List free from Duplicates:"); +list1.printList();