leetcode每日一题(3)

    科技2024-06-15  71

    反转链表

    public class ListNode{ ListNode next = null; int value; public ListNode(int data){ this.value=data; } } public static void reverseList(ListNode head){ ListNode pre = null;//记录上一个节点 ListNode temp = null;//临时存储变量 while(head!=null){ temp = head.next;//因为马上要对首个节点处理了,先把它下一个保存起来否则下面就找不到了 head.next=pre;//传入的首个节点的下一个节点变为它上一个节点 pre=head;//pre永远是最边上的头节点 head=temp;//head变为下一个节点,继续反转 } return pre; }
    Processed: 0.012, SQL: 8