Skip to content

Commit

Permalink
Add 206 reverse linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
escwxyz committed May 3, 2023
1 parent a5e240e commit 390c48f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions typescript/src/206-reverse-linked-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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
}
}

/**
* Problem: https://leetcode.com/problems/reverse-linked-list/
* @param head - linked list
* @returns reversed linked list
*/
export function reverseList(head: ListNode | null): ListNode | null {
if (head == null || head.next == null) return head

const last = reverseList(head.next)
head.next.next = head
head.next = null
return last
}

if (import.meta.vitest) {
const { test, expect } = import.meta.vitest
test('test one', () => {
expect(
reverseList(
new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4))))
)
).toStrictEqual(
new ListNode(4, new ListNode(3, new ListNode(2, new ListNode(1))))
)
})

test('test two', () => {
expect(reverseList(new ListNode(1))).toStrictEqual(new ListNode(1))
})
}

0 comments on commit 390c48f

Please sign in to comment.