c# - Recursive insert-method for a linked list -
i'm learning c# , i've made recursive insert-method linked list:
public static int recursiveinsert(ref int value, ref mylinkedlist list) { if (list == null) return new mylinkedlist(value, null); else { list.next = recursiveinsert(ref int value, ref list.next); return list; } }
how can modify method make recursive call this:
recursiveinsert(value, ref list.next)
instead of:
list.next = recursiveinsert(ref int value, ref list.next);
since never mutating parameters passing reference, can not pass them reference @ all. need recognize mylinkedlist
being reference type (it should absolutely reference type) means you're not passing value of object itself, you're passing reference it, can mutate reference without passing reference reference.) remove uses of ref
(and fix return type correct) , you're done:
public static mylinkedlist recursiveinsert(int value, mylinkedlist list) { if (list == null) return new mylinkedlist(value, null); else { list.next = recursiveinsert(value, list.next); return list; } }
Comments
Post a Comment