206. 反转链表
作者:
Andysys | 来源:发表于
2019-12-30 22:25 被阅读0次 // 迭代
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
ListNode temp = null;
while (curr != null) {
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
return prev;
}
// 递归
public ListNode reverseList2(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
本文标题:206. 反转链表
本文链接:https://www.haomeiwen.com/subject/ynbeoctx.html
网友评论