Reversing single linked list in C#

后端 未结 13 912
说谎
说谎 2020-12-07 17:02

I am trying to reverse a linked list. This is the code I have come up with:

 public static void Reverse(ref Node root)
 {
      Node tmp = root;
      Node n         


        
13条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 17:39

    The definition of ref is unnecessary because if you make the node as a reference type, it is OK to do:

    public static void Reverse(Node root)
    

    Also, the beauty of the interview question is less consumption of memory and in place reversal. Maybe a recursive way of doing it is also asked.

提交回复
热议问题