题目链接 这里
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null)
{
return null;
}
ListNode next=head.next;
ListNode pre=head;
ListNode temp;
head.next=null;
while(next!=null)
{
temp=next.next;
next.next=pre;
pre=next;
next=temp;
}
return pre;
}
}
本文介绍了一种使用Java实现单链表逆序的方法。通过迭代的方式改变链表节点的指向,最终达到逆序的效果。文章包含完整的代码实现,并详细解释了每一步操作的目的。
1424

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



