Description:
Reverse a singly linked list.
Solution:
正着读一遍,然后类似入栈即可。
import java.util.*;
public class Solution {
public ListNode reverseList(ListNode head) {
ListNode tail = null;
ListNode temp = head, next;
while (temp != null) {
next = temp.next;
temp.next = tail;
tail = temp;
if (next == null)
break;
temp = next;
}
return temp;
}
}