[LeetCode] #206 反转链表
给你单链表的头节点
head
,请你反转链表,并返回反转后的链表。 
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
迭代,需要一个临时变量先保存下一个节点
class Solution {
public ListNode reverseList(ListNode head) {
ListNode p1 = null,p2 = head;
while(p2 != null){
ListNode temp = p2.next;
p2.next = p1;
p1 = p2;
p2 = temp;
}
return p1;
}
}
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next == null) return head;
ListNode newListNode = reverseList(head.next);
head.next.next = head;
head.next = null;
return newListNode;
}
}