Reversing single linked list in C#

后端 未结 13 895
说谎
说谎 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

        public class Node
        {
            public T Value { get; set; }
            public Node Next { get; set; }
        } 
    
        public static Node Reverse(Node head)
        {
            Node tail = null;
    
            while(head!=null)
            {
                var node = new Node { Value = head.Value, Next = tail };
                tail = node;
                head = head.Next;
            }
    
            return tail;
        }
    

提交回复
热议问题