Skip to content

Commit dc2f955

Browse files
authored
Update 206-reverse-linked-list.js
1 parent 3ec5795 commit dc2f955

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

206-reverse-linked-list.js

+27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
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+
const reverseList = function(head) {
13+
if(head == null) return head
14+
const pre = new ListNode(null, head)
15+
let cur = head
16+
while(cur.next) {
17+
let tmp = pre.next
18+
pre.next = cur.next
19+
cur.next = cur.next.next
20+
pre.next.next = tmp
21+
}
22+
23+
return pre.next
24+
};
25+
26+
// another
27+
128
/**
229
* Definition for singly-linked list.
330
* function ListNode(val) {

0 commit comments

Comments
 (0)