题目
思路
- 三个节点,pre,head,next,然后互相换就可以了。
代码
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode pre = head;
head = head.next;
ListNode next = head.next;
pre.next=null;
while (head!=null){
head.next = pre;
pre = head;
head = next;
if(next!=null) next = next.next;
}
return pre;
}
}