I'm trying to delete a node in a singly linked list in a given position.When i submit this code all the test cases are success.But except one and the compiler shows abort called.When i googled it it shows resource exceeded.Is there is any other way to optimize this code to reduce the resource usage. I have written my code inside SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* head, int position) function.
You’re given the pointer to the head node of a linked list and the position of a node to delete. Delete the node at the given position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The list may become empty after you delete the node.
Input Format
You have to complete the deleteNode(SinglyLinkedListNode* llist, int position) method which takes two arguments - the head of the linked list and the position of the node to delete. You should NOT read any input from stdin/console. position will always be at least 0 and less than the number of the elements in the list.
The first line of input contains an integer , denoting the number of elements in the linked list. The next lines contain an integer each in a new line, denoting the elements of the linked list in the order. The last line contains an integer
denoting the position of the node that has to be deleted form the linked list.
Constraints
, where is the element of the linked list.
Output Format Delete the node at the given position and return the head of the updated linked list. Do NOT print anything to stdout/console.
The code in the editor will print the updated linked list in a single line separated by spaces.
#include <bits/stdc++.h>
using namespace std;
class SinglyLinkedListNode {
public:
int data;
SinglyLinkedListNode *next;
SinglyLinkedListNode(int node_data) {
this->data = node_data;
this->next = nullptr;
}
};
class SinglyLinkedList {
public:
SinglyLinkedListNode *head;
SinglyLinkedListNode *tail;
SinglyLinkedList() {
this->head = nullptr;
this->tail = nullptr;
}
void insert_node(int node_data) {
SinglyLinkedListNode* node = new SinglyLinkedListNode(node_data);
if (!this->head) {
this->head = node;
} else {
this->tail->next = node;
}
this->tail = node;
}
};
void print_singly_linked_list(SinglyLinkedListNode* node, string sep, ofstream& fout) {
while (node) {
fout << node->data;
node = node->next;
if (node) {
fout << sep;
}
}
}
void free_singly_linked_list(SinglyLinkedListNode* node) {
while (node) {
SinglyLinkedListNode* temp = node;
node = node->next;
free(temp);
}
}
// Complete the deleteNode function below.
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* head, int position) {
int i = 0;
SinglyLinkedListNode* temp = new SinglyLinkedListNode(2);
SinglyLinkedListNode* c;
SinglyLinkedListNode* p;
c = head;
p = head;
for(;i!=position;p=c,c=c->next,i++);
p->next = c->next;
delete c;
return head;
}
int main()