思路
使用递归实现
出口 ,遇到null
每一层要做:把下层放进去,把本层放下去
代码
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode result=new ListNode(0);
public ListNode resultCur=result;
public ListNode trainningPlan(ListNode head) {
if (head == null) {
return null;
}
ListNode digui = digui(head, head);
return digui;
}
public ListNode digui(ListNode realHead,ListNode head){
if (head == null) {
return result;
}
//将后面的放入
digui(realHead,head.next);
//将这个本身放入
resultCur.val = head.val;
if (head != realHead) {
//然后resultCur向后走
ListNode listNode = new ListNode();
resultCur.next = listNode;
}
resultCur = resultCur.next;
return result;
}
}