-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppendingNNodes.cpp
46 lines (42 loc) · 923 Bytes
/
AppendingNNodes.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//head is the head of the linked list and n is how many element you want to append from the last to the front
// Following is the node structure
/**************
class node{
public:
int data;
node * next;
node(int data){
this->data=data;
this->next=NULL;
}
};
***************/
int length(node* head){
int count=0;
node* temp = head ;
while(temp!=NULL){
temp=temp->next ;
count++ ;
}
return count ;
}
node* append_LinkedList(node* head,int n)
{
//write your code here
int result = length(head);
int count = result-n ;
int i=1 ;
node* current = head ;
while(i<count){
current=current->next ;
i++ ;
}
node* head2 = current->next ;
current->next=NULL ;
node* tail = head2 ;
while(tail->next!=NULL){
tail=tail->next ;
}
tail->next=head ;
return head2 ;
}