题目描述 输入一个链表,反转链表后,输出链表的所有元素。
思路: 设置三个指针,表示前、中、后。
注意特殊输入 链表为null的情况 只有一个节点的情况
public class Solution {
public ListNode
ReverseList(ListNode head) {
ListNode pre =
null, next =
null;
while(head !=
null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}