题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
两个链表解决:
public class Solution { public ListNode ReverseList(ListNode head) { if(head == null) return null; //定义新链表 ListNode reverseList = null;
while(head != null){
//保存下一节点和后面的所有节点
ListNode node = head.next;
//最关键的,从reverseList的头部插入
head.next = reverseList;
//reverseList更新
reverseList = head;
//赋值,将改变后的head只想燕来的下一个节点
head = node;
}
return reverseList;
}
}