题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
import java.util.*;
public class Solution {
public ListNode ReverseList(ListNode head) {
Stack<Integer> stack = new Stack<>();
while (head != null) {
stack.push(head.val);
head = head.next;
}
if (stack.isEmpty()) {
return null;
}
ListNode r = new ListNode(stack.pop());
ListNode p = r;
while (!stack.isEmpty()) {
r.next = new ListNode(stack.pop());
r = r.next;
}
r.next = null;
return p;
}
}