反转链表
递归方法
1 | public ListNode reverseList(ListNode head) { |
循环方法1
2
3
4
5
6
7
8
9
10public ListNode reverseList(ListNode head) {
ListNode cur = head, pre = null, next = null;
while(cur != null){
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}