问题:
解题:
分析:第一个元素的next->null,之后每次将下一个挪在第一个之前,挪到临时node为null
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode data) {
if (null== data) return null;
ListNode pre = data, temp = data.next, suffix;
pre.next = null;
while (null != temp) {
suffix = temp.next;
temp.next = pre;
pre = temp;
temp = suffix;
}
return pre;
}
}
性能:
不创建新对象,加快速度