WELCOME TO Pluto134340小行星

清风湿润,茶烟轻扬。

23.反转链表

LCR 024. 反转链表

给定单链表的头节点 head ,请反转链表,并返回反转后的链表的头节点。

示例 1:

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

image

/**
 * 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) {
        if(head==null) return null;
        ListNode tt = null;
        ListNode temp = head;
        ListNode headA = null;

        while(temp != null){
            tt=temp.next;
            temp.next = headA;
            headA=temp;
            temp=tt;
        }
        return headA;

    }
}
View Code
posted @ 2026-01-20 10:18  Pluto134340  阅读(0)  评论(0)    收藏  举报