非常经典的题目;
定义三个指针,cur指向当前操作的节点;next记录下一个要操作的节点;pre指向上一个操作的节点
/** * 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 head) { ListNode cur = head; ListNode pre = null; ListNode next = null; while(cur != null){ next = cur.next; cur.next = pre; pre = cur; cur = next; } return pre; } }