Skip to main content
added 50 characters in body; edited tags; edited title
Source Link
Der Kommissar
  • 20.3k
  • 4
  • 70
  • 158

coding style of one simple function Insert a node at a specific position in a Linked List

all. BelowThis code is my implementation of "Insertbuilt to insert a node atNode into a specific position inof a linked list"-list. It already passedpasses all the test cases. Could you plz help verify for the codeinterview, but I was wondering about the coding style regarding technique interviewtechniques, and especially the variable name?naming, and any other best-practices you can help with.

Node* InsertNth(Node *head, int data, int position)
{
    // Complete this method only
    // Do not write main function. 
    Node * sentinel = new Node();
    sentinel->data = -1;
    sentinel->next = head;
    Node* node = sentinel;
    int i = 0;
    while (node->next && i < position) {
        node = node->next;
        i++;
    }

    Node* preNext = node->next;
    Node* newNode = new Node();
    newNode->data = data;
    newNode->next = preNext;
    node->next = newNode;
    head = sentinel->next;
    delete sentinel;

    return head;

}

coding style of one simple function

all. Below is my implementation of "Insert a node at a specific position in a linked list". It already passed test cases. Could you plz help verify the code style regarding technique interview, especially the variable name?

Node* InsertNth(Node *head, int data, int position)
{
    // Complete this method only
    // Do not write main function. 
    Node * sentinel = new Node();
    sentinel->data = -1;
    sentinel->next = head;
    Node* node = sentinel;
    int i = 0;
    while (node->next && i < position) {
        node = node->next;
        i++;
    }

    Node* preNext = node->next;
    Node* newNode = new Node();
    newNode->data = data;
    newNode->next = preNext;
    node->next = newNode;
    head = sentinel->next;
    delete sentinel;

    return head;

}

Insert a node at a specific position in a Linked List

This code is built to insert a Node into a specific position of a linked-list. It passes all the test cases for the interview, but I was wondering about the coding style regarding techniques, and especially variable naming, and any other best-practices you can help with.

Node* InsertNth(Node *head, int data, int position)
{
    // Complete this method only
    // Do not write main function. 
    Node * sentinel = new Node();
    sentinel->data = -1;
    sentinel->next = head;
    Node* node = sentinel;
    int i = 0;
    while (node->next && i < position) {
        node = node->next;
        i++;
    }

    Node* preNext = node->next;
    Node* newNode = new Node();
    newNode->data = data;
    newNode->next = preNext;
    node->next = newNode;
    head = sentinel->next;
    delete sentinel;

    return head;

}
Source Link
nathan
  • 131
  • 1
  • 2

coding style of one simple function

all. Below is my implementation of "Insert a node at a specific position in a linked list". It already passed test cases. Could you plz help verify the code style regarding technique interview, especially the variable name?

Node* InsertNth(Node *head, int data, int position)
{
    // Complete this method only
    // Do not write main function. 
    Node * sentinel = new Node();
    sentinel->data = -1;
    sentinel->next = head;
    Node* node = sentinel;
    int i = 0;
    while (node->next && i < position) {
        node = node->next;
        i++;
    }

    Node* preNext = node->next;
    Node* newNode = new Node();
    newNode->data = data;
    newNode->next = preNext;
    node->next = newNode;
    head = sentinel->next;
    delete sentinel;

    return head;

}