K15
Reverse Linked List
Problem Statement & Objective
Given the head of a singly linked list, reverse the list, and return the reversed list.
Function Signature (Python)
reverse_linked_list(nodes: list[list[int]], head: int) -> listInput & Output Contract
Output Rule:[yeni_bas, sonraki_listesi]. Boş liste için [-1, []].
Örnek: [1→2→3] düğümleri [[1,1],[2,2],[3,-1]], bas = 0 iken cevap [2, [-1, 0, 1]] (3→2→1).
System Constraints
0 <= dugumler.length <= 5000
-5000 <= değer <= 5000
bas, 0..n-1 aralığında bir indeks veya boş liste için -1
Döngü yokturExample Execution Walkthrough
dugumler = [[1, 1], [2, 2], [3, -1]], bas = 0
1 → 2 → 3 ters çevrilince 3 → 2 → 1
yeni_bas = 2, sonraki = [-1, 0, 1]
dugumler = [[2, 1], [3, -1], [1, 0]], bas = 2
Zincir indeksleri 2 → 0 → 1 (değerler 1, 2, 3). Ham dizinin tersi bu sonucu vermez.
Ters çevirince 3 → 2 → 1, yeni_bas = 1, sonraki = [2, 0, -1]Example Test Scenarios (3)
Example 1: Sıralı Zincir
Input:
[[[1,1],[2,2],[3,3],[4,4],[5,-1]],0]Expected Output:
[4,[-1,0,1,2,3]]Example 2: Dizi Sırası Bağlantı Sırası Değil
Input:
[[[2,1],[3,-1],[1,0]],2]Expected Output:
[1,[2,0,-1]]Example 3: Empty List
Input:
[[],-1]Expected Output:
[-1,[]]