题目描述
输入一个链表,反转链表后,输出新链表的表头。
解题思路:
通过递归得方式完成此题。首先声明pre=ReverseList(head.next)也就是pre进行遍历整个链表。
当达到链表结尾开始进行反转head.next.next = head,其中 head.next = null;为防止形成一个环
- public class ListNodeReverse {
- public ListNode ReverseList(ListNode head) {
- if(head == null || head.next==null) return null;
- ListNode pre = ReverseList(head.next);
- head.next.next = head;
- head.next = null;
- return pre;
- }
- }
扫码关注一起随时随地学习!!!就在洋葱攻城狮,更多精彩,等你来!!