Reversing single linked list in C#

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

    You don't need to make a copy. Some pseudo code:

    prev = null;
    current = head;
    next = current->next;
    
    (while next != null)
        current->next=prev
        prev=current
        current=next
        next=current->next
    

提交回复
热议问题