#include <iostream>
using namespace std;
struct Node{
int data;
Node* pNext;
};
void insertAtBeginning(Node* &pHead, int x);
void insertAfterK(Node* pHead, int x, int k);
void insertBeforeK(Node* &pHead, int x, int k);
//-----------------------------------------------------//
void insertAtBeginning(Node* &pHead, int x) {
Node* newNode = new Node;
newNode->data = x;
newNode->pNext = pHead;
pHead = newNode;
}
//-----------------------------------------------------//
void insertAfterK(Node* pHead, int x, int k) {
while(pHead != nullptr && pHead->data != k)
pHead = pHead->pNext;
if(pHead == nullptr) return;
Node* newNode = new Node;
newNode->data = x;
newNode->pNext = pHead->pNext;
pHead->pNext = newNode;
}
//-----------------------------------------------------//
void insertBeforeK(Node*& pHead, int x, int k) {
if(!pHead) return;
Node* cur = pHead;
if(cur->data == k) {
insertAtBeginning(pHead, x);
return;
}
while(cur && cur->pNext && cur->pNext->data != k) {
cur = cur->pNext;
}
if(!cur->pNext) return;
Node* newNode = new Node;
newNode->data = x;
newNode->pNext = cur->pNext;
cur->pNext = newNode;
}
//-----------------------------------------------------//
int main() {}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IE5vZGV7CiAgICBpbnQgZGF0YTsKICAgIE5vZGUqIHBOZXh0Owp9OwoKdm9pZCBpbnNlcnRBdEJlZ2lubmluZyhOb2RlKiAmcEhlYWQsIGludCB4KTsKdm9pZCBpbnNlcnRBZnRlcksoTm9kZSogcEhlYWQsIGludCB4LCBpbnQgayk7CnZvaWQgaW5zZXJ0QmVmb3JlSyhOb2RlKiAmcEhlYWQsIGludCB4LCBpbnQgayk7CgovLy0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLy8KCnZvaWQgaW5zZXJ0QXRCZWdpbm5pbmcoTm9kZSogJnBIZWFkLCBpbnQgeCkgewogICAgTm9kZSogbmV3Tm9kZSA9IG5ldyBOb2RlOwogICAgbmV3Tm9kZS0+ZGF0Y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