Skip to content

Commit 675c7bb

Browse files
committed
add exercise 0206
1 parent 8e2abb2 commit 675c7bb

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

typescript/0206_reverse_linked_list/0206_reverse_linked_list.test.ts

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
13+
const reverseList = function (head: ListNode): ListNode {
14+
let node: ListNode | null = head;
15+
let prevNode: ListNode | null = null;
16+
let nextNode: ListNode | null = null;
17+
18+
while (node != null) {
19+
nextNode = node.next;
20+
node.next = prevNode;
21+
prevNode = node;
22+
node = nextNode;
23+
}
24+
25+
return prevNode!;
26+
};

0 commit comments

Comments
 (0)