Given the head of a singly linked list, reverse the list, and return the reversed list.
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null)
return null;
ListNode cur = head, pre = null;
while (cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
No comments:
Post a Comment