Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions longest-substring-without-repeating-characters/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* https://leetcode.com/problems/longest-substring-without-repeating-characters/
* time complexity : O(n)
* space complexity : O(n)
*/
function lengthOfLongestSubstring(s: string): number {
let n = s.length, ans = 0;
const map = new Map<string, number>();
for (let j = 0, i = 0; j < n; j++) {
if (map.has(s[j])) {
i = Math.max((map.get(s[j]) ?? 0) + 1, i);
}
ans = Math.max(ans, j - i + 1);
map.set(s[j], j);
}
return ans;
};
50 changes: 50 additions & 0 deletions reverse-linked-list/gitsunmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* https://leetcode.com/problems/reverse-linked-list
* time complexity : O(n)
* space complexity : O(1)
*/

class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val === undefined ? 0 : val)
this.next = (next === undefined ? null : next)
}
}

/** Not used in real problem */
const arrayToListNode = (arr: number[]): ListNode | null => {
if (arr.length === 0) return null;
let head = new ListNode(arr[0]);
let curr = head;
for (let i = 1; i < arr.length; i++) {
curr.next = new ListNode(arr[i]);
curr = curr.next;
}
return head;
}

/* Main function */
function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode | null = null;
let current = head;

while (current !== null) {
const next = current.next;
current.next = prev;
prev = current;
current = next;
}

return prev;
};

/* Examples */
const input1 = [1, 2, 3, 4, 5];
const input2 = [1, 2];
const input3 = [];

console.log('output1:', reverseList(arrayToListNode(input1)));
console.log('output2:', reverseList(arrayToListNode(input2)));
console.log('output3:', reverseList(arrayToListNode(input3)));