generated from pesto-students/PestoPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from pesto-students/week-7
Week 7
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const reverseList = head => { | ||
let prev = null; | ||
let next = null; | ||
let current = head | ||
while (current !== null){ | ||
next = current.next | ||
current.next = prev | ||
prev = current | ||
current = next | ||
} | ||
return prev | ||
} | ||
console.log(reverseList(1->2->3->4->5->6)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var rotateLinkedList = function (head, k) { | ||
var count = 1; | ||
var last = head; | ||
var now = head; | ||
|
||
if (!head || !head.next) return head; | ||
|
||
while (last.next) { | ||
last = last.next; | ||
count++; | ||
} | ||
|
||
k %= count; | ||
|
||
if (k === 0) return head; | ||
|
||
while (k < count - 1) { | ||
now = now.next; | ||
k++; | ||
} | ||
|
||
last.next = head; | ||
head = now.next; | ||
now.next = null; | ||
|
||
return head; | ||
}; | ||
console.log(rotateLinkedList([2, 4, 7, 8, 9], 5)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const hasLoop = (head) => { | ||
let set = new Set() | ||
let current = head | ||
|
||
while(current){ | ||
if(set.has(current)){ | ||
return true | ||
}else{ | ||
set.add(current) | ||
} | ||
|
||
current = current.next | ||
} | ||
return false | ||
}; | ||
console.log(hasLoop([1,8,3,4])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
function checkParanthesis(str) { | ||
let brackets = []; | ||
for (let i = 0; i < str.length; i++) { | ||
if (str[i] === "(") { | ||
brackets.push(str[i]); | ||
|
||
} else if (str[i] === ")") { | ||
if (brackets[brackets.length - 1] === "(") brackets.pop(); | ||
else brackets.push("#"); | ||
|
||
} | ||
|
||
} | ||
return true; | ||
} | ||
|
||
console.log(checkParanthesis('{([])}')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function printNGE(arr, n) { | ||
var next, i, j; | ||
for (i = 0; i < n; i++) { | ||
next = -1; | ||
for (j = i + 1; j < n; j++) { | ||
if (arr[i] < arr[j]) { | ||
next = arr[j]; | ||
break; | ||
} | ||
} | ||
console.log(arr[i]/n,next); | ||
|
||
} | ||
} | ||
console.log(printNGE([6,8,0,1,3],5)) |