Skip to content

Commit 961a21c

Browse files
On it
1 parent 8adc481 commit 961a21c

8 files changed

+58
-41
lines changed

LeetcodeProblems/Swap_Nodes_In_Pairs.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ Note:
1212
Your algorithm should use only constant extra space.
1313
You may not modify the values in the list's nodes, only nodes itself may be changed.
1414
*/
15-
16-
var ListNode = require('../UtilsClasses/ListNode').ListNode;
15+
const assert = require('assert');
16+
const ListNode = require('../UtilsClasses/ListNode').ListNode;
17+
const ListNodeTestHelper = require('../utilsClasses/ListNodeTestHelper');
1718

1819
/**
1920
* Definition for singly-linked list.
@@ -49,10 +50,17 @@ var swapPairs = function(head) {
4950
};
5051

5152
var main = function() {
52-
console.log(swapPairs(ListNode.linkenList([1,2,3,4])));
53-
console.log(swapPairs(ListNode.linkenList([])));
54-
console.log(swapPairs(ListNode.linkenList([1])));
55-
console.log(swapPairs(ListNode.linkenList([1,2])));
53+
54+
ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([2])), [2, 3]);
55+
test();
56+
}
57+
58+
var test = function () {
59+
// TODOOOOOOOOOO
60+
// ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2,3,4])), [2,1,4,3]);
61+
// ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([])), []);
62+
// ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1])), [1]);
63+
ListNodeTestHelper.assertList(swapPairs(ListNode.linkenList([1,2])), [2, 3]);
5664
}
5765

5866
module.exports.main = main;

LeetcodeProblems/Tic_Tac_Toe.js

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -67,35 +67,20 @@ var main = function() {
6767
module.exports.main = main;
6868

6969
ticTacToe = new TicTacToe();
70-
// ticTacToe.isBoardFull();
71-
// ticTacToe.addToken(0,1,"X");
72-
// ticTacToe.printBoard();
73-
// var iter = 0;
74-
// while(iter < 8) {
75-
// ticTacToe.makeMove();
76-
// iter++;
77-
// }
78-
79-
// console.log("after 8 moves");
80-
// ticTacToe.isBoardFull();
81-
// ticTacToe.printBoard();
82-
// ticTacToe.makeMove();
83-
84-
// ticTacToe.printBoard();
85-
// ticTacToe.addToken(0,0,"X");
86-
// ticTacToe.printBoard();
87-
88-
89-
90-
// // var readline = require('readline')
91-
92-
// // const rl = readline.createInterface({
93-
// // input: process.stdin,
94-
// // output: process.stdout
95-
// // });
70+
ticTacToe.isBoardFull();
71+
ticTacToe.addToken(0,1,"X");
72+
ticTacToe.printBoard();
73+
var iter = 0;
74+
while(iter < 8) {
75+
ticTacToe.makeMove();
76+
iter++;
77+
}
9678

97-
// // var response = rl.question('Whats your name : ', answer)
79+
console.log("after 8 moves");
80+
ticTacToe.isBoardFull();
81+
ticTacToe.printBoard();
82+
ticTacToe.makeMove();
9883

99-
// // function answer(response) {
100-
// // console.log(response)
101-
// // }
84+
ticTacToe.printBoard();
85+
ticTacToe.addToken(0,0,"X");
86+
ticTacToe.printBoard();

LeetcodeProblems/Unique_Binary_Search_Trees.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Given n = 3, there are a total of 5 unique BST's:
2222
DP Solution: https://www.programcreek.com/2014/05/leetcode-unique-binary-search-trees-java/
2323
*/
2424

25-
var assert = require('assert');
25+
const assert = require('assert');
2626

2727
// Solution 3 using DP
2828
var numTrees3 = function (n) {

LeetcodeProblems/Unique_Paths.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Output: 28
2626

2727
// Solution 1
2828
// This solution is a naive solution implementing a binary tree and visiting each node.
29-
var assert = require('assert');
29+
const assert = require('assert');
3030

3131
var uniquePaths1 = function(m, n) {
3232
return uniquePathsAux(0, 0, m, n)

LeetcodeProblems/Valid_Parentheses.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Input: "{[]}"
3232
Output: true
3333
*/
3434

35-
var assert = require('assert');
35+
const assert = require('assert');
3636

3737
var isValid = function(s) {
3838
var stack = [];

LeetcodeProblems/Verify_Preorder_Serialization_of_a_Binary_Tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Example 3:
3131
Input: "9,#,#,1"
3232
Output: false
3333
*/
34-
var assert = require('assert');
34+
const assert = require('assert');
3535

3636
/**
3737
* @param {string} preorder

utilsClasses/ListNode.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,24 @@ class ListNode {
1010

1111
var first = new ListNode(arr[0]);
1212
var head = first;
13-
for(var i = 1; i <= arr.length; i++) {
13+
for(var i = 1; i < arr.length; i++) {
1414
head.next = new ListNode(arr[i]);
1515
head = head.next;
1616
}
1717

1818
return first;
1919
}
20+
21+
length() {
22+
var list = this;
23+
var size = 0;
24+
while(list) {
25+
list = list.next;
26+
size++;
27+
}
28+
29+
return size;
30+
}
2031
}
2132

2233
module.exports.ListNode = ListNode;

utilsClasses/ListNodeTestHelper.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const assert = require('assert');
2+
3+
var assertList = function(list, expectedArr) {
4+
assert.strictEqual(list.length(), expectedArr.length);
5+
for(var i = 0; i < expectedArr.length; i++) {
6+
assert.strictEqual(list.val, expectedArr[i]);
7+
list = list.next;
8+
}
9+
10+
assert(list)
11+
}
12+
13+
module.exports.assertList = assertList;

0 commit comments

Comments
 (0)