15,Java递归--反转链表

反转链表

http://www.nowcoder.com/questionTerminal/75e878df47f24fdc9dc3e400ec6058ca

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;     
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务