链表206.反转链表
使用三个引用,不需要额外分配内存空间,直接撸
public ListNode reverseList(ListNode head) {
ListNode h1 = head;
if (head == null) {
return null;
}
<pre><code> if (h1.next == null) {
return h1;
}
ListNode h2 = h1.next;
ListNode h3 = h2.next;
h1.next = null;
while (h2 != null) {
h2.next = h1;
h1 = h2;
h2 = h3;
if (h3 != null) h3 = h3.next;
}
return h1;
}
评论区