diff --git a/Sum of Last N nodes of the Linked List.cpp b/Sum of Last N nodes of the Linked List.cpp new file mode 100644 index 0000000..db3fe71 --- /dev/null +++ b/Sum of Last N nodes of the Linked List.cpp @@ -0,0 +1,92 @@ +#include +using namespace std; + +struct node{ + int data; + struct node *next;// +}*head; + +void insertq(int a[],int m){ + struct node *temp,*newnode; + head=(struct node*)malloc(sizeof(struct node));// + + head->data=a[0]; + head->next=NULL; + + // temp=(struct node*)malloc(sizeof(struct node));; + temp=head; + //head->next=temp; + + for(int i=1;idata=a[i]; + newnode->next=NULL; + + temp->next=newnode; + // newnode=newnode->next; + temp=newnode; + } + + +} +void printq(struct node *head){ + + struct node*temp; + temp=head; + while(temp!=NULL){ + cout<data<<"\n"; + temp=temp->next; + } +} + + +int sumOfLastN_Nodes(struct node* head, int n) +{ + // Code here + if(n<=0) + return 0; + struct node*temp = head; + int m=0,sum=0; + while(temp!=NULL){ + m++; + temp=temp->next; + } + cout<<"m...."<next; + cout<<"head\n"<<"....."<data<data; + cout<<"sum here,,,"<next; + } + cout<<"sum\n"<<"....."<>m; + int a[m]; + cout<<"enter elements\n"; + for(int i=0;i>a[i]; + } + cout<<"enter nth\n"; + int n; + cin>>n; + cout<<"calling insert....\n"; + insertq(a,m); + cout<<"calling print....\n"; + printq(head); + int res=sumOfLastN_Nodes(head,n); + cout<<"print sum....\n"<