File tree 2 files changed +33
-0
lines changed
2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 47
47
151|[ Reverse Words in a String] ( ./0151-reverse-words-in-a-string.js ) |Medium|
48
48
152|[ Maximum Product Subarray] ( ./0152-maximum-product-subarray.js ) |Medium|
49
49
203|[ Remove Linked List Elements] ( ./0203-remove-linked-list-elements.js ) |Easy|
50
+ 206|[ Reverse Linked List] ( ./0206-reverse-linked-list.js ) |Easy|
50
51
217|[ Contains Duplicate] ( ./0217-contains-duplicate.js ) |Easy|
51
52
219|[ Contains Duplicate II] ( ./0219-contains-duplicate-ii.js ) |Easy|
52
53
226|[ Invert Binary Tree] ( ./0226-invert-binary-tree.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments