Preguntas de cepillado de Leetcode (lista de enlaces únicos) 1 - lista de enlaces inversos

206. Lista de enlaces inversos

Invertir una lista enlazada individualmente.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    
    
    public ListNode reverseList(ListNode head) {
    
    
        if(head == null)
            return head;
        ListNode next = head.next;
        head.next = null;
        while(next != null){
    
    
            ListNode next2 = next.next;
            next.next = head;
            head = next;
            next = next2;
        }
        return head;
    }
}

Supongo que te gusta

Origin blog.csdn.net/qq_38754625/article/details/108521525
Recomendado
Clasificación