剑指Offer_15 发表于 2017-12-01 | 分类于 剑指Offer 题目输入一个链表,反转链表后,输出链表的所有元素。 解题思路三个指针来记录位置,反转链表。 12345678910111213141516171819202122232425262728293031/*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; ListNode cur = head; ListNode post = head.next; while(post != null){ cur.next = pre; pre = cur; cur = post; post = post.next; } //循环退出条件post == null //最后一个节点还未指向它的前一节点 cur.next = pre; return cur; }}