Acwing-35:反转链表(Java语言实现)

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode p = head;
        int listLength = 0;
        ListNode newLN = new ListNode(-1);
        while(p != null){
            listLength++;
            p = p.next;
        }
        int[] nums = new int[listLength];
        p = head;
        int j = 0;
        while(p != null){
            nums[j++] = p.val;
            p = p.next;
        }
        p = newLN;
        for (int i = listLength-1 ; i>=0;i--){

            p.next = new ListNode(nums[i]);
            p = p.next;
        }

        return newLN.next;
    }
}

你可能感兴趣的:(链表,算法,数据结构,笔记,java)