反转链表

75 阅读1分钟

使用双链表思想,把原链表的节点都摘掉,每次摘掉的链表都让他成为新链表的头节点,然后更新新链表。

public class Solution{
    public ListNode ReverseList(ListNode head){
        ListNode newHead = null;
        while(head != null){
            ListNode temp = head.next;
            head.next = newHead;
            newHead = head;
            head = temp;
        }
        return newHead;
    }
}