Reversing single linked list in C#

后端 未结 13 660
说谎
说谎 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:41

    public Node ReverseList(Node cur, Node prev)
        {
            if (cur == null) // if list is null
                return cur;
    
            Node n = cur.NextNode;
            cur.NextNode = prev;
            return (n == null) ? cur : ReverseList(n, cur);
        }
    

提交回复
热议问题