Cisco Systems Interview Question
Software Engineer / DevelopersTeam: Switching Softwares
Country: United States
Interview Type: Phone Interview
Recursive version
bool DeleteNode(Linklist p,int a)
{
static Linklist head=NULL;
if(head==NULL)
{
head=p;
if(head->data==a)
{
return 1;
}
}
if(p==NULL) return 0;
if(p->next==0) return 0;
if(a!=p->next->data)
{
return DeleteNode(p->next,a);
}
else
{
Linklist q=p->next;
p->next=q->next;
free(q);
return 1;
}
}
node* deleteNode (node* head, int val ) {
node *ptr1, *ptr2 ;
if ( head == NULL )
return head ;
else if ( head->data == val ) {
head = head->next ;
return head ;
}
ptr1 = head ;
ptr2 = head->next ;
while ( ptr2 ) {
if ( ptr2->data == val ) {
ptr1->next = ptr2->next ;
return head ;
}
ptr1 = ptr2 ;
}
return head ;
}
node* deleteNode (node* head, int val ) {
node *ptr1, *ptr2 ;
if ( head == NULL )
return head ;
else if ( head->data == val ) {
head = head->next ;
return head ;
}
ptr1 = head ;
ptr2 = head->next ;
while ( ptr2 ) {
if ( ptr2->data == val ) {
ptr1->next = ptr2->next ;
return head ;
}
ptr1 = ptr2 ;
}
return head ;
}
struct SLNode {
SLNode *next;
int data;
};
void SL_delete_node(SLNode *head, int value)
{
SLNode *prev = NULL;
while (head){
if (head->data) { // found
if (NULL == prev) { // this is the only node
printf("The value is in head of SLL\n");
return;
}
else { // we have a previous node
prev->next = head->next;
//Delete the current node
delete head;
break;
}
}
prev = head;
head = head->next;
}
}
// Function returns pointer to first node of updated list
Node* RemoveNode(Node* pFirstNode, int nValue){
if(nullptr == pFirstNode){
return pFirstNode;
}
if(pFirstNode->i == nValue){
Node* pReturn = pFirstNode->next;
free(pFirstNode);
return pReturn;
}
Node *pCurrent = pFirstNode;
while((nullptr != pCurrent) && (nullptr != pCurrent->next)){
if(pCurrent->next->i == nValue){
Node* pTemp = pCurrent->next;
pCurrent->next = pCurrent->next->next;
free(pTemp);
}
pCurrent = pCurrent->next;
}
return pFirstNode;
}
/* Returns pointer to the deleted node.
* Adjust head if needed.
*/
node*
del_node(node **head, int number) {
node* c, p; #current and previous node
for (c = *head, p = NULL; c != NULL; c = c->next, p = c)
if (c->data == number)
if (p != NULL)
p->next = c->next;
else
*head = c->next;
break;
return c;
}
void List::mQuestion2(int aValue)
{
class Node *lTemp = mHead;
if(lTemp == NULL)
return;
if(lTemp->mData == aValue)
{
mHead = lTemp->mNext;
lTemp->mNext = NULL;
//delete lTemp
mNoOfElements--;
}
else
{
while(lTemp->mNext !=NULL && lTemp->mNext->mData != aValue)
{
lTemp = lTemp->mNext;
}
if(lTemp->mNext == NULL)
{
cout << "Element not found" <<endl;
return;
}
class Node *lTemp2 = lTemp->mNext;
lTemp->mNext = lTemp->mNext->mNext;
lTemp2->mNext = NULL;
//delete lTemp2;
mNoOfElements--;
}
}
say ptr1 points to start of linked list,
- sp!de? March 21, 2012if(ptr1==null) return false;
node * ptr2;
ptr2=ptr1->next;
if(ptr1->data==number){
ptr1=ptr2;
return true;
}
while(ptr2){
if(ptr2->data==number){
ptr1->next=ptr2->next;
return true;
}
ptr1=ptr2;
ptr2=ptr2->next;
}
return false;