Solution 1
0ms 38.79
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode cur;
cur = head;
ListNode next = cur.next;
cur.next = null;
ListNode nextNext;
while(next != null){
nextNext = next.next;
next.next = cur;
cur = next;
next = nextNext;
}
return cur;
}
}
Solution2 Use dummy
0ms 38.79%
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null){
return head;
}
ListNode dummy = new ListNode(-1);
ListNode next = head;
while(next != null){
ListNode temp = next.next;
next.next = dummy.next;
dummy.next = next;
next = temp;
}
return dummy.next;
}
}