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

Popular posts from this blog

C# random value from dictionary and tuple -

cgi - How do I interpret URLs without extension as files rather than missing directories in nginx? -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -