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;
}
}
Acwing-35:反转链表(Java语言实现)
于 2024-01-03 17:25:00 首次发布