reverse list (recursive)

来源:互联网 发布:js 扫描条形码 编辑:程序博客网 时间:2024/06/06 17:05
/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode reverseList(ListNode head) {        if (head == null || head.next == null) {            return head;        }        ListNode second = head.next;        head.next = null;        ListNode rest = reverseList(second);        second.next = head;        return rest;    }}

0 0