Skip to content

Commit 8889e72

Browse files
Raziyehbazargandawsbot
authored andcommitted
Chapter 2: Remove dups (careercup#9)
* find middle node of a Singly Linked List * find middle node of a Singly Linked List * find middle node of a Singly Linked List * add find-middle node tests * Delete middle Node of Linked List * correct2-3-Delete Middle Node * add solution for access to the head / or access to middle-node * 2-2: rturn Kth to Last * add for loop for craating new node in beforeEach for tests * 2-8: Detection Loop in SLL * 2-1-remove duplicate nodes
1 parent 07eb1be commit 8889e72

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [ ] 1.9 - String Rotation
1717

1818
#### Chapter 2
19+
- [x] 2.1 - [Remove Duplicate](lib/data-structures/chapter-2/2_1.js)
1920
- [x] 2.2 - [Return Kth to Last](lib/data-structures/chapter-2/2_2.js)
2021
- [x] 2.3 - [Delete Middle Node](lib/data-structures/chapter-2/2_3.js)
2122
- [x] 2.8 - [Detection Loop](lib/data-structures/chapter-2/2_8.js)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var SLL = require('./helper.js');
2+
3+
SLL.prototype.removeDups = function(head) {
4+
5+
if (head === null || head.next === null) {
6+
return null;
7+
}
8+
9+
var current = head;
10+
11+
while(current !== null) {
12+
var runner = current;
13+
while (runner.next !== null) {
14+
if (runner.next.data === current.data) {
15+
runner.next = runner.next.next;
16+
this.length--;
17+
} else {
18+
runner = runner.next;
19+
}
20+
}
21+
current = current.next;
22+
}
23+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require('../../test_helper');
2+
3+
describe('2.1 #removeDups', function () {
4+
describe('remove duplicate from linkedlist', function () {
5+
var sll;
6+
beforeEach(function() {
7+
sll = new MyLinkedList();
8+
for(var i=1; i < 6; i++) {
9+
sll.addNode( i + 'Node', null);
10+
}
11+
//create duplicate in linkedlist
12+
sll.addNode('5Node', null);
13+
});
14+
15+
afterEach(function() {
16+
sll = null;
17+
});
18+
19+
it('should delete duplicate node from SLL', function () {
20+
sll.removeDups(sll.head);
21+
expect(sll).length.to.be(5);
22+
});
23+
});
24+
});

0 commit comments

Comments
 (0)