Reverse a Linked List Using Recursion

This problem involves reversing a linked list using recursion. The given example input is a list of integers, but this technique can be applied to lists of any data type. The output should be the reverse of the input list.

Problem

Given a linked list, reverse the nodes in the list.
Example input:
1->2->3->4->5
Example output:
5->4->3->2->1

Solution

This solution is in C#. Our A.I. can create solutions in multiple languages.
by kayakane
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            LinkedList<int> list = new LinkedList<int>();
            list.AddLast(1);
            list.AddLast(2);
            list.AddLast(3);
            list.AddLast(4);
            list.AddLast(5);

            LinkedList<int> reversedList = ReverseLinkedList(list);

            foreach (int i in reversedList)
            {
                Console.WriteLine(i);
            }
        }

        public static LinkedList<int> ReverseLinkedList(LinkedList<int> list)
        {
            LinkedList<int> reversedList = new LinkedList<int>();

            foreach (int i in list)
            {
                reversedList.AddFirst(i);
            }

            return reversedList; }
    }
}

A.I. Evaluation of the Solution

The candidate's solution correctly reverses the nodes in a linked list. The approach is straightforward and easy to understand. The code is well-organized and easy to read.

Evaluated at: 2022-12-06 08:15:37