1 static void Main(string[] args) 2 { 3 while (true) 4 { 5 LinkedList L = new LinkedList(); 6 L.Add(new Node("first")); 7 L.Add(new Node("second")); 8 L.Add(new Node("third")); 9 L.Add(new Node("forth")); 10 Cw(L.Head); 11 L.Head= Reverse(L.Head); 12 Cw(L.Head); 13 Console.ReadKey(); 14 i = 1; 15 } 16 17 } 18 19 private static int i = 1; 20 21 22 //单向链表逆序 23 private static Node Reverse(Node header) 24 { 25 if (header.Next == null) 26 { 27 return header; 28 } 29 //找到最后一个 30 var node = Reverse(header.Next); 31 //后一个的指针 指向前一个 32 header.Next.Next = header; 33 //前一个指针置空 34 header.Next = null; 35 return node; 36 } 37 38 //单向链表输出 39 private static void Cw(Node node) 40 { 41 Console.WriteLine(node.Data); 42 if (node.Next != null) 43 { 44 Cw(node.Next); 45 } 46 } 47 48 public class Node 49 { 50 public object Data; 51 public Node Next; 52 public Node(object Data) 53 { 54 this.Data = Data; 55 } 56 } 57 58 public class LinkedList 59 { 60 public Node Head; 61 public Node Tail; 62 public void Add(Node n) 63 { 64 if (Head == null) 65 { 66 Head = n; 67 Tail = Head; 68 } 69 else 70 { 71 Tail.Next = n; 72 Tail = Tail.Next; 73 } 74 } 75 }
来源:https://www.cnblogs.com/TeemoHQ/p/11250174.html