题目描述
输入一个链表,反转链表后,输出新链表的表头。
源代码:
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
//post指向头结点的下一个节点,然后把头结点所指结点的next域指向前一个结点。
public ListNode ReverseList(ListNode head) {
ListNode pre=null;
ListNode post=null;
if(head == null) return null;
while(head!=null){
post=head.next;
head.next=pre;
pre=head;
head=post;
//head=head.next;
}
return pre;
}
}
本文深入探讨了链表数据结构的反转算法实现,通过具体的Java代码示例,详细讲解了如何利用三个指针(pre、head、post)来实现链表的就地反转,为读者提供了清晰的算法思路和代码实现细节。
1087

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



