C# simple and fast O(n)


  • 0
    B
    public class Solution 
    {
        public ListNode ReverseList(ListNode head)
        {
            ListNode seeker = head;
            var valStack = new Stack<int>();
    
            while (seeker != null)
            {
                valStack.Push(seeker.val);
                seeker = seeker.next;
            }
            seeker = head;
            while (seeker != null)
            {
                seeker.val = valStack.Pop();
                seeker = seeker.next;
            }
            return head;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.