Reversing single linked list in C#

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

    Why not just have the head point at the tail, the tail point at the head, and go through the list reversing the direction in which prev points?

    If you're not using a head and a tail, just go through the list reversing the prev relationships, and then make head point at the one that had a null prev when you got to it.

提交回复
热议问题