Reversing single linked list in C#

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

    This performed pretty well on Leetcode.

    public ListNode ReverseList(ListNode head) {
    
            ListNode previous = null;
            ListNode current = head; 
            while(current != null) {
                ListNode nextTemp = current.next;
                current.next = previous;
                previous = current;
                current = nextTemp;
            }
    
            return previous;
        }     
    

提交回复
热议问题