AcWing 17. Java代码——头插法
原题链接
简单
作者:
迪迦奥特曼大战BAT
,
2020-08-25 21:16:09
,
所有人可见
,
阅读 2
Java代码
class Solution {
public int[] printListReversingly(ListNode head) {
ListNode newHead = new ListNode(-1);
ListNode tmpNode = head;
// tmpNode = head;
int count = 0;
while(head!=null){
head = head.next;
tmpNode.next = newHead.next;
newHead.next = tmpNode;
tmpNode = head;
// head = head.next;
count = count + 1;
}
int[] resultArray = new int[count];
int i = 0;
newHead = newHead.next;
while(newHead!=null){
resultArray[i++] = newHead.val;
//System.out.println(newHead.val);
newHead = newHead.next;
}
return resultArray;
}
}