-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinked List K Append.cpp
68 lines (61 loc) · 1.12 KB
/
Linked List K Append.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;
class Node{
public:
const int data;
Node*next;
Node(int d): data(d), next(NULL){}
~Node(){}
};
void insertAfterHead(Node*&head, int data){
if(head==NULL){
head = new Node(data);
return;
}
Node*n = new Node(data);
Node*ptr = head;
while(ptr->next!=NULL){
ptr = ptr->next;
}
ptr->next=n;
return;
}
void AppendLL(Node*&head, int k){
if (head == NULL || head->next == NULL){
return;
}
Node*current = NULL;
Node*Next = head;
while(k--){
while(Next->next != NULL){
current = Next;
Next = Next->next;
}
current->next = NULL;
Next->next = head;
head = Next;
}
}
void printLL(Node*head){
while(head!=NULL){
cout<<head->data<<" ";
head = head->next;
}
cout<<endl;
}
int main(int argc, char const *argv[])
{
Node*head = NULL;
int n, data;
cin>>n;
for(int i=0; i<n; i++){
cin>>data;
insertAfterHead(head, data);
}
int k;
cin>>k;
//printLL(head);
AppendLL(head, k);
printLL(head);
return 0;
}