-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.ts
68 lines (62 loc) · 1.63 KB
/
19.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Definition for singly-linked list.
* 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)
* }
* }
*/
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)
}
}
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
if (head.next == null) return null
if (head.next.next == null) {
if (n == 2) return head.next
head.next = null
return head
}
let left = null
let right = null
right = head
for (let i = 0; i < n; i++) {
right = right.next
}
while (right) {
left = left == null ? head : left.next
right = right.next
}
if (left) {
right = left.next?.next
left.next = right ? right : null
} else {
head = head.next
}
return head
};
function printList(mergeList: ListNode) {
while (mergeList != null) {
console.log(mergeList.val)
mergeList = mergeList.next
}
console.log(" ")
}
function makeList(first: number[]) {
if (first.length == 0) return null
let list = new ListNode(first[first.length - 1])
for (let i = first.length - 2; i >= 0; i--) {
const temp = new ListNode(first[i])
temp.next = list
list = temp
}
return list
}
printList(removeNthFromEnd(makeList([1, 2, 3, 4, 5]), 5))