Skip to content

Commit 48df976

Browse files
committed
Add solution #206
1 parent 2e3432b commit 48df976

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
4848
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
4949
203|[Remove Linked List Elements](./0203-remove-linked-list-elements.js)|Easy|
50+
206|[Reverse Linked List](./0206-reverse-linked-list.js)|Easy|
5051
217|[Contains Duplicate](./0217-contains-duplicate.js)|Easy|
5152
219|[Contains Duplicate II](./0219-contains-duplicate-ii.js)|Easy|
5253
226|[Invert Binary Tree](./0226-invert-binary-tree.js)|Easy|

solutions/0206-reverse-linked-list.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 206. Reverse Linked List
3+
* https://leetcode.com/problems/reverse-linked-list/
4+
* Difficulty: Easy
5+
*
6+
* Given the head of a singly linked list, reverse the list, and return the reversed list.
7+
*/
8+
9+
/**
10+
* Definition for singly-linked list.
11+
* function ListNode(val, next) {
12+
* this.val = (val===undefined ? 0 : val)
13+
* this.next = (next===undefined ? null : next)
14+
* }
15+
*/
16+
/**
17+
* @param {ListNode} head
18+
* @return {ListNode}
19+
*/
20+
var reverseList = function(head) {
21+
let prev = null;
22+
let tail = head;
23+
24+
while (tail) {
25+
const next = tail.next;
26+
tail.next = prev;
27+
prev = tail;
28+
tail = next;
29+
}
30+
31+
return prev;
32+
};

0 commit comments

Comments
 (0)