/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
//空表或链表长度为1
if(head == null || head.next == null)
return head;
ListNode preLN = null;//前结点
ListNode nextLN = null;//next结点
while(head != null){
nextLN = head.next;//保存当前结点的next结点,防止链表断裂
head.next = preLN;//翻转链表当前结点指向
preLN = head;//当前结点成为前结点,
head = nextLN;//链表右移,继续处理原链表的下一个结点
}
return preLN;
}
}
该博客介绍了如何使用迭代方式实现链表的反转。通过定义前结点和next结点,逐步更新链表节点的指向,最终完成链表的反转操作。此算法适用于单链表的处理,对于理解链表操作和数据结构有帮助。
434

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



