Reverse a singly linked list.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode reverseList(ListNode head) { //需要画图,注意每个节点的起始位置,注意第一个rear为null if(head==null||head.next==null) return head; ListNode rear=null; ListNode p=head; ListNode pre=head; while(pre!=null){ pre=pre.next; p.next=rear; rear=p; p=pre; } return rear; } }