/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode ReverseList(ListNode head) { if(head == null || head.next == null) return head; ListNode pre = null;//注意要考虑到第一个节点,让它指向null ListNode next = null; while(head != null){ next = head.next; //先记录一下当前节点的下一个节点,以防断开找不到下一个节点 head.next = pre; pre = head; head = next; } return pre; } }
原文:https://www.cnblogs.com/HuangYJ/p/13463106.html