天天看点

剑指offer JZ15 反转链表

题目链接:

JZ15 反转链表

本题思路:

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
    	// 递归
        // base case
        if(head == null || head.next == null) return head;
        
        // 走到链表的末端结点
        ListNode newNode = ReverseList(head.next);
        
        // 再将当前节点设置为 后面节点 的后续节点
        head.next.next = head;
        head.next = null;
        
        return newNode;
    }
}
           

继续阅读