Files
2026-06-13 04:08:10 +10:00

56 lines
807 B
Go

package top100liked
import (
"fmt"
"testing"
)
// https://leetcode.cn/problems/reverse-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
type ListNode struct {
Val int
Next *ListNode
}
func reverseList(head *ListNode) *ListNode {
if head == nil {
return nil
}
var last *ListNode = nil
h := head
for h.Next != nil {
tmp := h.Next
h.Next = last
last = h
h = tmp
}
h.Next = last
return h
}
func TestS23(t *testing.T) {
input := ListNode{
Val: 1,
Next: &ListNode{
Val: 2,
Next: &ListNode{
Val: 3,
Next: &ListNode{
Val: 4,
Next: &ListNode{
Val: 5,
Next: nil,
},
},
},
},
}
output := reverseList(&input)
for output.Next != nil {
fmt.Println(output.Val)
output = output.Next
}
fmt.Println(output.Val)
}