1 solutions

  • 0
    @ 2026-10-22 18:20:38

    C :

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int getLoop(char* str, int posLoop) {
       
        int i = 0;
    
        char* tmp = str;
        while (str != 0) {
            tmp = str;
            while (*str != ',' && *str != 0) {
                str++;
            }
            if (*str == ',') {
                *str = 0;
                str++;
                int data = atoi(tmp);
                if (i++ == posLoop)
                    return data;
            }
            else {
                int data = atoi(tmp);
                if (i++ == posLoop)
                    return data;
                break;
            }
        }
    
        return -1;
    }
    
    
    int main() {
        char str[100] = { 0 };
        int posLoop = 0;
        scanf("%s", str);
        scanf("%d", &posLoop);
        int len = 0;
        int data = getLoop(str, posLoop);
    
    
    
        if (data == -1)
            printf("null\n");
        else
            printf("%d\n", data);
    
        return 0;
    }
    

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1e6 + 10;
    #define int long long
    #define endl '\n' 
    typedef pair<int,int> PII;
    
    void solve()
    {
        string line;
        getline(cin, line);
        stringstream ss(line);
        vector<int> a;
        int num;
        char comma;
        while (ss >> num) 
        {
            a.push_back(num);
            if (ss >> comma) continue;
        }
    
        int k;
        cin >> k;
    
        if(k == -1) cout << "null" << endl;
        else cout << a[k] << endl;
    
    }
    
    signed main()
    {
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    
        int xiao_p;
        xiao_p = 1;
    
        while(xiao_p --)
        {
            solve();
        }
    
        return 0;
    }
    

    Java :

    import java.util.Scanner;
    
    class LN {
        int val;
        LN next;
        LN(int x) { val = x; }
    }
    
    public class Main {
        public static void main(String[] args) {
            try{
                Scanner sc = new Scanner(System.in);
                String[] ns = sc.nextLine().trim().split(",");
                int cs = sc.nextInt();
    
                LN hd = buildLN(ns, cs);
                LN cy = findCy(hd);
    
                if (cy == null) {
                    System.out.println("null");
                } else {
                    System.out.println(cy.val);
                }
            }catch (Exception e){
                System.out.println("null");
            }
    
        }
    
        private static LN buildLN(String[] ns, int cs) {
            if (ns == null || ns.length == 0) {
                return null;
            }
    
            LN dm = new LN(0);
            LN cur = dm;
            LN[] nds = new LN[ns.length];
    
            for (int i = 0; i < ns.length; i++) {
                nds[i] = new LN(Integer.parseInt(ns[i].trim()));
                cur.next = nds[i];
                cur = cur.next;
            }
    
            if (cs >= 0 && cs < ns.length) {
                cur.next = nds[cs];
            }
    
            return dm.next;
        }
    
        private static LN findCy(LN hd) {
            if (hd == null || hd.next == null) {
                return null;
            }
    
            LN sl = hd;
            LN fs = hd;
    
            while (fs != null && fs.next != null) {
                sl = sl.next;
                fs = fs.next.next;
                if (sl == fs) {
                    break;
                }
            }
    
            if (fs == null || fs.next == null) {
                return null;
            }
    
            sl = hd;
            while (sl != fs) {
                sl = sl.next;
                fs = fs.next;
            }
    
            return sl;
        }
    }
    
    

    Python :

    # coding=utf-8
    
    class ListNode:
        def __init__(self, x):
            self.val = x
            self.next = None
    
    def detectCycle(head):
        slow = fast = head
        # 第一步:判断是否有环
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
            if slow == fast:
                # 第二步:找到环的入口
                entry = head
                while entry != slow:
                    entry = entry.next
                    slow = slow.next
                return entry
        return None
    
    # 用户输入链表节点值
    values = input().strip()  # 链表节点值,逗号分隔
    cycle_index = int(input())  # 环的位置,-1 表示没有环
    
    # 处理输入格式
    if values == "":  # 空链表情况
        nodes = []
    else:
        values = list(map(int, values.split(',')))
        nodes = [ListNode(val) for val in values]
    
    # 构造链表
    for i in range(len(nodes) - 1):
        nodes[i].next = nodes[i + 1]
    
    # 创建环(如果存在)
    if cycle_index != -1 and nodes:
        nodes[-1].next = nodes[cycle_index]  # 设置环的入口
    
    # 检测环的入口
    result = detectCycle(nodes[0] if nodes else None)
    if result:
        print(result.val)  # 输出环的入口节点值
    else:
        print("null")  # 输出 "null" 代表无环或空链表
    
    
    • 1

    Information

    ID
    350
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By