/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre =null;
ListNode cur = head;
while(cur != null){
ListNode next = cur.next;
cur.next = pre;
pre =cur;
cur = next;
}
return pre;
}
}
自我总结
每次遍历的时候,要new出来下一个节点。判断条件是当前不为null,到末尾的下一个节点,判断条件就成立,所以返回上一个节点pre即可。