Java 递归
例:链表1->2->3->4
我们把2->3->4递归成4->3->2.不过,1这个节点我们并没有去碰它,所以1的next节点仍然
是连接着2,接下来就简单了,我们接下来只需要把节点2的nxet指向1,然后把1的next指向
null
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode node = ReverseList(head.next);
ListNode two = head.next;
two.next = head;
head.next = null;
return node;
}
}
京公网安备 11010502036488号