给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null) return null ;
ListNode a = head;
ListNode b = a.next;
a.next = null;
while(b!=null){
ListNode c = b.next;
b.next = a;
a = b;
b = c;
}
head = a;
return head;
}
}
这篇博客详细介绍了如何使用迭代方式反转单链表,通过 ListNode 类定义链表节点,利用三个指针 a, b, c 实现链表的反转操作。在不断迭代中更新节点的 next 指针,最终返回反转后的链表头节点。
1092

被折叠的 条评论
为什么被折叠?



