1
/**
2
* Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode() {}
3
* ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val;
4
* this.next = next; } }
5
*/
6
class Solution {
7
public ListNode reverseList(ListNode head) {
8

9
if (head == null || head.next == null) return head;
10

11
ListNode curr = head;
12
ListNode temp = null, next = curr.next;
13
curr.next = null;
14

15
while (curr != null && next != null) {
16
// before cutting off link between next & its next, save next.next
17
temp = next.next;
18
// let next point to curr
19
next.next = curr;
20

21
curr = next;
22
next = temp;
23
}
24

25
return curr;
26
}
27
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0