public ListNode reverse(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode res = new ListNode(-1);
        ListNode ptr = res;
        res.next = head;
        while (head != null) {
            ListNode temp = head.next;
            head.next = ptr;
            ptr = head;
            head = temp;
        }
        res.next.next = null;
        return ptr;
    }