题意:输入单链表,实现逆序
思路:注意出现死循环:一般造成原因是忽略头节点的next指针未指向null(注意边界情况)
public ListNode reverseList(ListNode head) {
//iteratively
if(head == null) return head;
ListNode next, tp;
next = head.next;
head.next = null; // avoid dead circle for 1th and 2th
while(next != null){
tp = head;
head = next;
next = next.next;
head.next = tp; //modify linkage
}
return head;
}