206.反转链表

点击查看代码
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode curr = head;

        while(curr != null) {
            ListNode nextTemp = curr.next;

            curr.next = prev;

            prev = curr;
            curr = nextTemp;
            
        }

        return prev;
    }
}
posted @ 2026-02-05 10:56  AnoSky  阅读(7)  评论(0)    收藏  举报