Given the
head
of a singly linked list, reverse the list, and return the reversed list.给你单链表的头节点
head
,请你反转链表,并返回反转后的链表。Example 1:
Input: head = [1,2,3,4,5] Output: [5,4,3,2,1]Example 2:
Input: head = [1,2] Output: [2,1]Example 3:
Input: head = [] Output: []Constraints:
- The number of nodes in the list is the range
[0, 5000]
.-5000 <= Node.val <= 5000
这一题的方法也有两种思路,一种是迭代法,不过就有点魔幻,我没想出来。
假设链表为1→2→3→∅,我们想要把它改成∅←1←2←3。这个时候我们还需要创建另外一个结点,用于对数据的交换,下面看看代码
class Solution {
public ListNode reverseList(ListNode head) {
ListNode prev = null;//创建一个空节点,没有指向
ListNode cur = head;//创建头结点指针
while(cur != null){
ListNode newNode = cur.next;//新结点指向头结点的下一个结点
cur.next = prev;
pre = cur;
cur = newNode;
}
return prev;//注意此时不能返回head,返回的话输出结果为空
}
}
/**
* 以链表1->2->3->4->5举例
* @param head
* @return
*/
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) {
/*
直到当前节点的下一个节点为空时返回当前节点
由于5没有下一个节点了,所以此处返回节点5
*/
return head;
}
//递归传入下一个节点,目的是为了到达最后一个节点
ListNode newHead = reverseList(head.next);
/*
第一轮出栈,head为5,head.next为空,返回5
第二轮出栈,head为4,head.next为5,执行head.next.next=head也就是5.next=4,
把当前节点的子节点的子节点指向当前节点
此时链表为1->2->3->4<->5,由于4与5互相指向,所以此处要断开4.next=null
此时链表为1->2->3->4<-5
返回节点5
第三轮出栈,head为3,head.next为4,执行head.next.next=head也就是4.next=3,
此时链表为1->2->3<->4<-5,由于3与4互相指向,所以此处要断开3.next=null
此时链表为1->2->3<-4<-5
返回节点5
第四轮出栈,head为2,head.next为3,执行head.next.next=head也就是3.next=2,
此时链表为1->2<->3<-4<-5,由于2与3互相指向,所以此处要断开2.next=null
此时链表为1->2<-3<-4<-5
返回节点5
第五轮出栈,head为1,head.next为2,执行head.next.next=head也就是2.next=1,
此时链表为1<->2<-3<-4<-5,由于1与2互相指向,所以此处要断开1.next=null
此时链表为1<-2<-3<-4<-5
返回节点5
出栈完成,最终头节点5->4->3-2->1
*/
head.next.next = head;
head.next = null;
return newHead;
}