建立虚拟节点进行链表反转
public static ListNode reverseList(ListNode head){
ListNode ans = new ListNode(-1);
ListNode cur = head;
while (cur != null){
ListNode next = cur.next;
cur.next = ans.next;
ans.next = cur;
cur = next;
}
return ans.next;
}
直接操作链表
public ListNode reverseList1(ListNode head){
ListNode prev = null;
ListNode cur = head;
while (cur != null){
ListNode next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
博客介绍了链表反转的两种方式,一是建立虚拟节点进行链表反转,二是直接操作链表。链表反转是数据结构中的重要操作,在信息技术领域有广泛应用。
441

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



