forked from hijiangtao/LeetCode-with-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathres.js
More file actions
36 lines (32 loc) · 631 Bytes
/
res.js
File metadata and controls
36 lines (32 loc) · 631 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Reverse a singly linked list.
*
* res.js
* @authors Joe Jiang (hijiangtao@gmail.com)
* @date 2017-02-25 21:12:51
* @version $Id$
*/
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
let reverseList = function(head) {
if (!head) {
return head;
}
let res = new ListNode(head.val);
while (head.next) {
let temp = res;
res = new ListNode(head.next.val);
res.next = temp;
head = head.next;
}
return res;
};