题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/* public class ListNode { int val; ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/ public class Solution { public ListNode ReverseList(ListNode head) { ListNode temp = null;//反转后的下一个节点head.next 初始值为空 相当于原最后的null ListNode next;//存放反转前的head.next if(head ==null){return null;} while(head!=null){ next = head.next;//反转之前把head.next存放在next中 防止指针断裂后找不到 head.next = temp;//把指针指向tenp temp = head;//由下一代码反推 head = next;//把要反转的节点往下移 } return temp; } }

