Delete node at specific position

Delete Node Hacker Rank 

Delete node snippet

static SinglyLinkedListNode deleteNode(SinglyLinkedListNode llist, int position)
    {
        var temp = llist;
        if(position==0)
        {
            return llist.next;
        }
        var i=0;
        while(i < position-1)
        {
            temp=temp.next;
            i++;
        }
        var current  = temp.next.next;
        temp.next=current;
        
        return llist;

    }

Full Code Snippets

Since there is no any instance of Result class created inside Main method so,

I have removed class result and make deleteNode method not public

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    class SinglyLinkedListNode {
        public int data;
        public SinglyLinkedListNode next;

        public SinglyLinkedListNode(int nodeData) {
            this.data = nodeData;
            this.next = null;
        }
    }

    class SinglyLinkedList {
        public SinglyLinkedListNode head;
        public SinglyLinkedListNode tail;

        public SinglyLinkedList() {
            this.head = null;
            this.tail = null;
        }

        public void InsertNode(int nodeData) {
            SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);

            if (this.head == null) {
                this.head = node;
            } else {
                this.tail.next = node;
            }

            this.tail = node;
        }
    }

    static void PrintSinglyLinkedList(SinglyLinkedListNode node, string sep, TextWriter textWriter) {
        while (node != null) {
            textWriter.Write(node.data);

            node = node.next;

            if (node != null) {
                textWriter.Write(sep);
            }
        }
    }



    /*
     * Complete the 'deleteNode' function below.
     *
     * The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
     * The function accepts following parameters:
     *  1. INTEGER_SINGLY_LINKED_LIST llist
     *  2. INTEGER position
     */

    /*
     * For your reference:
     *
     * SinglyLinkedListNode
     * {
     *     int data;
     *     SinglyLinkedListNode next;
     * }
     *
     */

    static SinglyLinkedListNode deleteNode(SinglyLinkedListNode llist, int position)
    {
        var temp = llist;
        if(position==0)
        {
            return llist.next;
        }
        var i=0;
        while( i< position-1)
        {
            temp=temp.next;
            i++;
        }
        var current  = temp.next.next;
        temp.next=current;
        
        return llist;

    }

    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        SinglyLinkedList llist = new SinglyLinkedList();

        int llistCount = Convert.ToInt32(Console.ReadLine());

        for (int i = 0; i < llistCount; i++) {
            int llistItem = Convert.ToInt32(Console.ReadLine());
            llist.InsertNode(llistItem);
        }

        int position = Convert.ToInt32(Console.ReadLine());

        SinglyLinkedListNode llist1 = deleteNode(llist.head, position);

        PrintSinglyLinkedList(llist1, " ", textWriter);
        textWriter.WriteLine();

        textWriter.Flush();
        textWriter.Close();
    }
}

Comments

Popular Posts