题目: 翻转链表
思路:
1. head一直指向头节点
2. p 则是从头到尾遍历链表
public class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) return head;
// head一直指向头节点, 而p 则是从头到尾遍历链表
ListNode p = head.next;
// 第一个节点next = null
head.next = null;
while(p != null) {
ListNode tmp = p.next;
p.next = head;
head = p;
p = tmp;
}
return head;
}
}