Poon
BAN USER
I love programming. The idea behind some technology intrigued me. I am trying my best to be a best programmer :)
stack seems to be the best solution here.
eg: 4, 6, -10, 8, 9, 10, -19, 10, 18, 20 25
int cancellationList(struct node* root)
{
stack<int>s;
temp=root;
while(temp)
{
if(temp->data > 0)
{
s.push(temp->data);
//temp=temp->next;
}
else if(arr[i]<0)
{
int x=s.pop();
while( (arr[i]-x) !=0) && (/**check for stack not empty**/)
{
x +=s.pop();
}
//temp=temp->next;
}
temp=temp->next;
}
int result=0;
if(/**stack empty**/)
return result;
while(/**stack not empty**/)
{
result += s.pop();
}
return result;
}
struct node* f, fp, s, sp;
//f= required node from start, fp= previous of required node from start, s= required node from end, sp=previous of required node from end
//to get from start
fp=root;
k=k-1;
whie(k)
{
fp=f; //1
f=f->next; //2
}
//to get from last: since k=2, if you go till k->next->next != null, it will always return last 2nd node from end
s=root;
while(s->next->next)
{
sp=s->next; //5
s=s->next->next; //7
}
//now we have links to both nodes, just swap them
f->next=sp->next->next; //2->8
s->next=fp->next->next; //7->4
fp->next=s; //1->7
sp->next=f; //5->2
//solution gives correct result but i'm not sure if it is advisable to answer in interviews in this way. Please do give suggestions :)
I think it is quite easy:
struct node* temp=root;
while(temp->next->next)
{
struct node* prev=null;
struct node* current=root;
while(current->next)
{
prev=current;
current=current->next;
}
current->next=temp->next;
prev->next=null;
temp->next=current;
temp=current->next;
}
}
Please comment if anything seems wrong.
RepCecilRenteria, Managing editor at Alliance Global Servies
A Managing Editor, or Content Manager, I" m creates content strategies and oversees their implementation processes. spent 2/3 years ...
Reprobinroi335, Android test engineer at ABC TECH SUPPORT
I believe the deepest changes you can make to improve your quality of life. how to break a family curse ...
Repammiwilson5, Personnel at BMO Harris Bank
Hi I am Ammy from Served on a research team for improved customer satisfaction survey process,Moderated focus groups to ...
Repjudydschultz1234, Android test engineer at Eterno Infotech Pvt Ltd
Spent a weekend researching weebles in Nigeria. My current pet project is developing strategies for how to break someone's ...
#include <iostream>
- Poon August 06, 2016#include <stdio.h>
#include <conio.h>
bool checkIsdigit(char* c)
{
if((c>=a) && (c<=z)) //or if(c>=0 && c<=9) return true else false;
return false;
return true;
}
int calculateSumFromString(string s)
{
int result=0; intermediateres=0;
for(int i=0;i<s.length(); i++)
{
if(checkIsdigit(s[i])
{
intermediateres=intermediateres*10+ (int) (s[i]); //or we can use atoi(s[i]): atoi converts string to integer
}
else
{
result=result + intermediateres;
intermediateres=0;
}
}
result=result + intermediateres;
return result;
}
int main()
{
return calculateSumFromString("11aa22bb33dd44");
}