File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * class ListNode {
4+ * val: number
5+ * next: ListNode | null
6+ * constructor(val?: number, next?: ListNode | null) {
7+ * this.val = (val===undefined ? 0 : val)
8+ * this.next = (next===undefined ? null : next)
9+ * }
10+ * }
11+ */
12+
13+ // Runtime: 0ms
14+ // Memory: 58.86MB
15+
16+ function reverseList ( head : ListNode | null ) : ListNode | null {
17+ let previousNode : ListNode | null = null ;
18+ let currentNode : ListNode | null = head ;
19+ while ( currentNode ) {
20+ const nextNode : ListNode | null = currentNode . next ; // mark nextNode's value of current node temporary
21+ currentNode . next = previousNode ; // set next node of current as previous node
22+ previousNode = currentNode ; // set previous node as current node
23+ currentNode = nextNode ; // move to nextNode
24+ }
25+ return previousNode // currentNode would be null if it reaches the end of the node list
26+ } ;
You can’t perform that action at this time.
0 commit comments