反序链表
两次遍历,一次存,一次拼接 public ListNode reverseList(ListNode head) { if(head==null){ return null; } List<ListNode> list = new ArrayList<ListNode>(); //1.遍历链表 ListNode p = head; while(p!=null){ //2.保存每一次遍历的结果 list.add(p); p=p.next; } ListNode r_head=null; //3.从List中取出链表并拼接 for(int i=0;i<list.size();i++){ ListNode n = list.get(i); //逆序凭借链表 n.next = r_head; r_head=n; } return r_head; } 递归 public ListNode reverseList(ListNode head) { if(head==null){ return null; } //终止条件:该节点是最后一个节点 if(head.next==null){ return head; } //求解子问题 ListNode p = reverseList(head.next); //将该节点拼在最后,完成逆序 head.next.next = head; head