File tree Expand file tree Collapse file tree 8 files changed +202
-3
lines changed
lib/data-structures/chapter-2
test/data-structures/chapter-2 Expand file tree Collapse file tree 8 files changed +202
-3
lines changed Original file line number Diff line number Diff line change 44
55## Question List:
66
7- #### Chapter 1
7+ #### Chapter 1
88- [x] 1.1 - [ Is Unique] ( lib/data-structures/chapter-1/1_1.js )
99- [x] 1.2 - [ Check Permutation] ( lib/data-structures/chapter-1/1_2.js )
1010- [x] 1.3 - [ URLify] ( lib/data-structures/chapter-1/1_3.js )
1313- [x] 1.6 - [ String Compression] ( lib/data-structures/chapter-1/1_6.js )
1414- [x] 1.7 - [ Rotate Matrix] ( lib/data-structures/chapter-1/1_7.js )
1515- [x] 1.8 - [ Zero Matrix] ( lib/data-structures/chapter-1/1_8.js )
16- - [ ] 1.9 - String Rotation
16+ - [ ] 1.9 - String Rotation
17+
18+ #### Chapter 2
19+ - [x] 2.2 - [ Return Kth to Last] ( lib/data-structures/chapter-2/2_2.js )
20+ - [x] 2.3 - [ Delete Middle Node] ( lib/data-structures/chapter-2/2_3.js )
21+ - [x] 2.8 - [ Detection Loop] ( lib/data-structures/chapter-2/2_8.js )
22+
1723
1824#### Chapter 3
1925- [ ] 3.1 - Three in One
@@ -38,7 +44,7 @@ npm install
3844npm test
3945```
4046
41- If you just want to lint
47+ If you just want to lint
4248``` bash
4349npm run lint
4450```
Original file line number Diff line number Diff line change 1+ var SLL = require ( './helper.js' ) ;
2+
3+ SLL . prototype . nthToLast = function ( head , k ) {
4+ var p1 = head ,
5+ p2 = head ;
6+
7+ for ( var i = 0 ; i < k ; i ++ ) {
8+ if ( p1 === null ) {
9+ return null ;
10+ }
11+ p1 = p1 . next ;
12+ }
13+
14+ while ( p1 !== null ) {
15+ p1 = p1 . next ;
16+ p2 = p2 . next ;
17+ }
18+ return p2 ;
19+ } ;
Original file line number Diff line number Diff line change 1+ var SLL = require ( './helper.js' ) ;
2+
3+ function Node ( data , next ) {
4+ this . data = data ,
5+ this . next = next ;
6+ }
7+
8+ SLL . prototype . addNode = function ( data , next ) {
9+ var node = new Node ( data , next ) ,
10+ current = this . head ;
11+
12+ if ( this . head === null ) {
13+ this . head = node ;
14+ this . length ++ ;
15+ return ;
16+ }
17+ while ( current . next ) {
18+ current = current . next ;
19+ }
20+ current . next = node ;
21+ this . length ++ ;
22+ } ;
23+
24+ //solution for delete middle node with access to the head of LinkedList
25+ SLL . prototype . findMiddleAndRemove = function ( head ) {
26+ ( head === null || head . next === null ) ? null : head ;
27+
28+ var slowPointer = head ,
29+ fastPointer = head ,
30+ previous = null ;
31+
32+ while ( fastPointer . next !== null && fastPointer . next . next !== null ) {
33+ fastPointer = fastPointer . next . next ;
34+ previous = slowPointer ;
35+ slowPointer = slowPointer . next ;
36+ }
37+
38+ previous . next = slowPointer . next ;
39+ this . length -- ;
40+ } ;
41+
42+ //solution for delete middle node with access to the node ( middle node ) not the head of LinkedList
43+ SLL . prototype . removeMiddleNode = function ( currentNode ) {
44+ if ( currentNode === null || currentNode . next === null ) {
45+ return false ;
46+ }
47+ //copy next node data to current node
48+ var nextNode = currentNode . next ;
49+ currentNode . data = nextNode . data ;
50+ currentNode . next = nextNode . next ;
51+
52+ //remove next node
53+ currentNode . next = nextNode . next ;
54+ this . length -- ;
55+ } ;
56+
57+ //return MyLinkedList;
Original file line number Diff line number Diff line change 1+ var SLL = require ( './helper.js' ) ;
2+
3+ SLL . prototype . findBeginning = function ( head ) {
4+ var slow = head ,
5+ fast = head ;
6+
7+ //find meeting point
8+ while ( fast !== null && fast . next !== null ) {
9+ slow = slow . next ;
10+ fast = fast . next . next ;
11+ if ( slow === fast ) {
12+ break ;
13+ }
14+ }
15+
16+ // no loop
17+ if ( fast === null || fast . next === null ) {
18+ return null ;
19+ }
20+
21+ // move slow to head. keep fast at meeting point. if they move at the same pace, they must meet at the loop start.
22+ slow = head ;
23+ while ( slow !== fast ) {
24+ slow = slow . next ;
25+ fast = fast . next ;
26+ }
27+ return fast ;
28+ } ;
Original file line number Diff line number Diff line change 1+ module . exports = MyLinkedList = ( function ( ) {
2+
3+ function MyLinkedList ( ) {
4+ this . head = null ;
5+ this . length = 0 ;
6+ }
7+
8+ return MyLinkedList ;
9+ } ) ( ) ;
Original file line number Diff line number Diff line change 1+ require ( '../../test_helper' ) ;
2+
3+ describe ( '2.2 #nthToLast' , function ( ) {
4+ describe ( 'return Kth element to last' , 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+ } ) ;
12+
13+ afterEach ( function ( ) {
14+ sll = null ;
15+ } ) ;
16+
17+ it ( 'should return Kth element to last' , function ( ) {
18+ var kthNode = sll . nthToLast ( sll . head , 3 ) ;
19+ expect ( kthNode ) . to . not . be . null ;
20+ expect ( kthNode ) . to . have . property ( 'data' ) . and . be . equal ( '3Node' ) ;
21+ } ) ;
22+ } ) ;
23+ } ) ;
Original file line number Diff line number Diff line change 1+ require ( '../../test_helper' ) ;
2+
3+ describe ( '2.3 #FindMiddle' , function ( ) {
4+ describe ( 'return middle Node' , 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+ } ) ;
12+
13+ afterEach ( function ( ) {
14+ sll = null ;
15+ } ) ;
16+
17+ it ( 'should return correct length of LinkedList' , function ( ) {
18+ expect ( sll ) . length . to . be ( 5 ) ;
19+ } ) ;
20+
21+ it ( 'should remove the middle node with aceess to the head of SLL' , function ( ) {
22+ sll . findMiddleAndRemove ( sll . head ) ;
23+ expect ( sll ) . length . to . be ( 4 ) ;
24+ } ) ;
25+
26+ it ( 'should remove the middle node with access to the middle_node not head' , function ( ) {
27+ sll . removeMiddleNode ( sll . head . next . next ) ;
28+ expect ( sll ) . length . to . be ( 4 ) ;
29+ } ) ;
30+
31+ } ) ;
32+ } ) ;
Original file line number Diff line number Diff line change 1+ require ( '../../test_helper' ) ;
2+
3+ describe ( '2.8 #findBeginning' , function ( ) {
4+ describe ( 'find loop in Singly 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 loop in linkedlist
12+ sll . addNode ( '6Node' , sll . head . next . next . next ) ;
13+ } ) ;
14+
15+ afterEach ( function ( ) {
16+ sll = null ;
17+ } ) ;
18+
19+ it ( 'should return loop start (node) in SLL' , function ( ) {
20+ var loopPoint = sll . findBeginning ( sll . head ) ;
21+ expect ( loopPoint ) . not . be . null ;
22+ expect ( loopPoint ) . to . have . property ( 'data' ) . and . be . equal ( '4Node' ) ;
23+ } ) ;
24+ } ) ;
25+ } ) ;
You can’t perform that action at this time.
0 commit comments