C# Solution


  • 0
        public ListNode RemoveNthFromEnd(ListNode head, int n) {
                if (head == null)
                    return null;
                
                ListNode dummyNode = new ListNode(-1), 
                         tempNode1 = dummyNode, 
                         tempNode2 = dummyNode;
                tempNode1.next = head;
    
                for(int i = 1; i <= n + 1; i++)
                    tempNode2 = tempNode2.next;
    
                while (tempNode2 != null)
                {
                    tempNode1 = tempNode1.next;
                    tempNode2 = tempNode2.next;
                }
    
                tempNode1.next = tempNode1.next.next;
    
                return dummyNode.next;
        }

Log in to reply
 

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