常用的循环

发布时间 2023-12-06 19:17:23作者: 二声

读取链表数据

using System;

public class ListNode
{
    public int val;
    public ListNode next;

    public ListNode(int val = 0, ListNode next = null)
    {
        this.val = val;
        this.next = next;
    }
}

class Program
{
    static void Main()
    {
        // 创建一个链表
        ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4))));

        // 遍历并读取链表的数据
        Console.WriteLine("链表的数据:");

        ListNode current = head;
        while (current != null)
        {
            Console.WriteLine(current.val);
            current = current.next;
        }
    }
}