【算法笔记】反转链表(Java实现)

算法 / 2023-08-09
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

public ListNode ReverseList(ListNode head){
    ListNode curr = new ListNode(-1);
    curr.next = head;
    ListNode next = head;
    ListNode temp = head;

    while (next.next!=null){
        next = next.next;

        temp.next = curr;
        curr = temp;

        temp = next;
    }

    next.next = curr;
    head.next = null;

    return next;
}