/**
* Definition of linked list
* class Node {
*
* public:
* int data;
* Node* next;
* Node() : data(0), next(nullptr) {}
* Node(int x) : data(x), next(nullptr) {}
* Node(int x, Node* next) : data(x), next(next) {}
* };
*/
Node* constructLL(vector<int>& arr) {
Node* head=new Node(arr[0]);
Node* mover=head;
for(int i=1;i<arr.size();i++){
Node* temp=new Node(arr[i]);
mover->next=temp;
mover=mover->next;
}
return head;
}
/**
* Definition of linked list
* class Node {
*
* public:
* int data;
* Node* next;
* Node() : data(0), next(nullptr) {}
* Node(int x) : data(x), next(nullptr) {}
* Node(int x, Node* next) : data(x), next(next) {}
* };
*/
Node* insertAtFirst(Node* list, int newValue) {
Node* temp=new Node(newValue);
temp->next=list;
list =temp;
return list;
}
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
void printList(Node* node) {
while (node != NULL) {
cout << node->data << " ";
node = node->next;
}
cout << "\n";
}
// } Driver Code Ends
/*Structure of the linked list node is as
struct Node {
int data;
struct Node * next;
Node(int x) {
data = x;
next = NULL;
}
}; */
class Solution {
public:
Node *insertAtEnd(Node *head, int x) {
Node *add=new Node(x);
if(!head) return add;
Node *temp=head;
while(temp->next){
temp=temp->next;
}
temp->next=add;
return head;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> arr;
string input;
getline(cin, input);
stringstream ss(input);
int number;
// Read numbers from the input line
while (ss >> number) {
arr.push_back(number);
}
int x;
cin >> x;
cin.ignore();
Node* head = nullptr;
// Check if the array is empty
if (!arr.empty()) {
head = new Node(arr[0]);
Node* tail = head;
for (size_t i = 1; i < arr.size(); ++i) {
tail->next = new Node(arr[i]);
tail = tail->next;
}
}
Solution ob;
Node* ans = ob.insertAtEnd(head, x);
printList(ans);
cout << "~\n";
}
return 0;
}
// } Driver Code Ends
/*
Following is the class structure of the Node class:
class Node
{
public:
int data;
Node *next;
Node()
{
this->data = 0;
next = NULL;
}
Node(int data)
{
this->data = data;
this->next = NULL;
}
Node(int data, Node* next)
{
this->data = data;
this->next = next;
}
};
*/
Node * deleteHead(Node *head) {
// Write your code here.
Node* temp=head;
head=head->next;
free(temp);
return head;
}
/**
* Definition for singly-linked list.
* class Node {
* public:
* int data;
* Node *next;
* Node() : data(0), next(nullptr) {}
* Node(int x) : data(x), next(nullptr) {}
* Node(int x, Node *next) : data(x), next(next) {}
* };
*/
Node *deleteLast(Node *list){
Node* temp=list;
if(!temp->next) return nullptr;
while(temp->next->next){
temp=temp->next;
}
free(temp->next);
temp->next=nullptr;
return list;
}
/****************************************************************
Following is the class structure of the Node class:
class Node
{
public:
int data;
Node *next;
Node()
{
this->data = 0;
next = NULL;
}
Node(int data)
{
this->data = data;
this->next = NULL;
}
Node(int data, Node* next)
{
this->data = data;
this->next = next;
}
};
*****************************************************************/
int length(Node *head)
{
//Write your code here
Node* temp=head;
int length=0;
while(temp){
temp=temp->next;
length++;
}
return length;
}
/****************************************************************
Following is the class structure of the Node class:
template <typename T>
class Node
{
public:
T data;
Node<T> *next;
Node()
{
this->data = 0;
this->next = NULL;
}
Node(T data)
{
this->data = data;
this->next = NULL;
}
Node(T data, T* next)
{
this->data = data;
this->next = next;
}
};
*****************************************************************/
int searchInLinkedList(Node<int> *head, int k) {
// Write your code here.
// int length=0;
Node<int>* temp=head;
while(temp){
if(temp->data==k) return 1;
temp=temp->next;
// length++;
}
return 0;
}
/*
* Definition for doubly-linked list.
* class Node
* {
* public:
* int data;
* Node *next, *prev;
* Node() : data(0), next(nullptr), prev(nullptr) {}
* Node(int x) : data(x), next(nullptr), prev(nullptr) {}
* Node(int x, Node *next, Node *prev) : data(x), next(next), prev(prev) {}
* };
*/
Node* constructDLL(vector<int>& arr) {
// Write your code here
Node* head=new Node(arr[0]);
Node* curr=head;
for(int i=1;i<arr.size();i++){
Node* newNode=new Node(arr[i]);
curr->next=newNode;
newNode->prev=curr;
curr=newNode;
}
return head;
}
/**
* Definition of doubly linked list:
*
* struct Node {
* int value;
* Node *prev;
* Node *next;
* Node() : value(0), prev(nullptr), next(nullptr) {}
* Node(int val) : value(val), prev(nullptr), next(nullptr) {}
* Node(int val, Node *p, Node *n) : value(val), prev(p), next(n) {}
* };
*
*************************************************************************/
Node * insertAtTail(Node *head, int k) {
Node* newNode=new Node(k);
if(!head) return newNode;
// Write your code here
Node* temp=head;
while(temp->next){
temp=temp->next;
}
temp->next =newNode;
newNode->prev=temp;
return head;
}
/**
* Definition of doubly linked list:
*
* struct Node {
* int data;
* Node *prev;
* Node *next;
* Node() : data(0), prev(nullptr), next(nullptr) {}
* Node(int val) : data(val), prev(nullptr), next(nullptr) {}
* Node(int val, Node *p, Node *n) : data(val), prev(p), next(n) {}
* };
*
*************************************************************************/
Node * deleteLastNode(Node *head) {
// Write your code here
Node* temp=head;
if(!temp->next) return nullptr;
if(!temp->next->next){
temp->next=nullptr;
return head;
}
while(temp->next->next){
temp=temp->next;
}
temp->next=nullptr;
return head;
}
/*
Following is the class structure of the Node class:
class Node
{
public:
int data;
Node *next,*prev;
Node()
{
this->data = 0;
next = NULL;
prev= NULL;
}
Node(int data)
{
this->data = data;
this->next = NULL;
this->prev= NULL;
}
Node(int data, Node* next, Node *prev)
{
this->data = data;
this->next = next;
this->prev= prev;
}
};
*/
#include <bits/stdc++.h>
Node* reverseDLL(Node* head)
{
if(!head || !head->next) return head;
Node* curr=head;
Node* prev=nullptr;
while(curr){
prev=curr->prev;
curr->prev=curr->next;
curr->next=prev;
curr=curr->prev;
}
return prev->prev;
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* middleNode(ListNode* head) {
if (!head || !head->next) return head;
ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* temp=head;
ListNode* prev=nullptr;
while(temp){
ListNode* front=temp->next;
temp->next=prev;
prev=temp;
temp=front;
}
return prev;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head || !head->next) return head;
ListNode* nextList=reverseList(head->next);
ListNode* front=head->next;
front->next=head;
head->next=nullptr;
return nextList;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
unordered_map<ListNode*,bool> mp;
ListNode*temp=head;
while(temp){
if(mp.find(temp)!=mp.end()) return true;
mp[temp]=1;
temp=temp->next;
}
return false;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode* slow=head;
ListNode* fast=head;
while(fast && fast->next){
slow=slow->next;
fast=fast->next->next;
if(slow==fast) return true;
}
return false;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
unordered_map<ListNode*,int> mp;
ListNode* temp=head;
int x=0;
while(temp){
if(mp.find(temp)!=mp.end()) return temp;
mp[temp]=x;
x++;
temp=temp->next;
}
return temp;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* slow=head,*fast=head;
while(fast && fast->next){
fast=fast->next->next;
slow=slow->next;
if(slow==fast){
slow=head;
while(slow!=fast){
slow=slow->next;
fast=fast->next;
}
return slow;
}
}
return nullptr;
}
};
/**
* Definition of linked list:
*
* class Node {
* public:
* int data;
* Node *next;
*
* Node(int data) {
* this->data = data;
* this->next = NULL;
* }
* };
*
*************************************************************************/
#include <bits/stdc++.h>
int lengthOfLoop(Node *head) {
// Write your code here
unordered_map<Node*,int> mp;
Node* temp=head;
int position=0;
while(temp){
if(mp.find(temp)!=mp.end()) return position-mp[temp];
mp[temp]=position;
position++;
temp=temp->next;
}
return 0;
}
/**
* Definition of linked list:
*
* class Node {
* public:
* int data;
* Node *next;
*
* Node(int data) {
* this->data = data;
* this->next = NULL;
* }
* };
*
*************************************************************************/
#include <bits/stdc++.h>
int findLength(Node*slow,Node* fast){
int count=1;
fast=fast->next;
while(fast!=slow){
fast=fast->next;
count++;
}
return count;
}
int lengthOfLoop(Node *head) {
// Write your code here
Node* slow=head,*fast=head;
while(fast && fast->next){
slow=slow->next;
fast=fast->next->next;
if(slow==fast) return findLength(slow,fast);
}
return 0;
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
stack<int> st;
ListNode* temp=head;
while(temp){
st.push(temp->val);
temp=temp->next;
}
while(head){
int x=st.top();
st.pop();
if(x!=head->val) return false;
head=head->next;
}
return true;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverse(ListNode* node){
ListNode* temp=node;
ListNode* prev=nullptr;
while(temp){
ListNode* next=temp->next;
temp->next=prev;
prev=temp;
temp=next;
}
return prev;
}
bool isPalindrome(ListNode* head) {
if(!head || !head->next) return true;
ListNode* slow=head;
ListNode* fast=head->next->next;
while(fast && fast->next){
fast=fast->next->next;
slow=slow->next;
}
slow=slow->next;
ListNode* rev= reverse(slow);
while(rev){
if(rev->val!=head->val) return false;
rev=rev->next;
head=head->next;
}
return true;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(!head || !head->next) return head;
ListNode* even=head,*odd=head;
vector<int> v1,v2;
odd=odd->next;
while(even->next && even->next->next){
v1.push_back(even->val);
even=even->next->next;
}
v1.push_back(even->val);
while(odd->next && odd->next->next){
v2.push_back(odd->val);
odd=odd->next->next;
}
v2.push_back(odd->val);
ListNode* ans=new ListNode(0);
ListNode* temp=ans;
for(int i=0;i<v1.size();i++){
temp->next= new ListNode(v1[i]);
temp=temp->next;
}
for(int i=0;i<v2.size();i++){
temp->next= new ListNode(v2[i]);
temp=temp->next;
}
return ans->next;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(!head || !head->next) return head;
ListNode* even=head->next,*odd=head;
ListNode* evenHead=even;
while(even && even->next){
odd->next=odd->next->next;
even->next=even->next->next;
odd=odd->next;
even=even->next;
}
odd->next=evenHead;
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
int count=0;
ListNode* temp=head;
while(temp){
temp=temp->next;
count++;
}
ListNode* temp1 = head;
count=(count-n);
if(count==0){
ListNode* toDelete=head;
head=head->next;
delete toDelete;
return head;
}
while(count>1){
temp1=temp1->next;
count--;
}
ListNode* toDelete=temp1->next;
temp1->next=temp1->next->next;
delete toDelete;
return head;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummy=new ListNode(0,head);
ListNode* fast=dummy;
ListNode* slow=dummy;
for(int i=0;i<=n;i++){
fast=fast->next;
}
while(fast){
fast=fast->next;
slow=slow->next;
}
ListNode* delNode=slow->next;
slow->next=delNode->next;
delete delNode;
return dummy->next;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* deleteMiddle(ListNode* head) {
if(!head || !head->next) return nullptr;
ListNode* slow=head;
ListNode* fast=head->next->next;
while(fast && fast->next){
fast=fast->next->next;
slow=slow->next;
}
slow->next=slow->next->next;
return head;
}
};
/*
Following is the class structure of the Node class:
class Node
{
public:
int data;
Node *next;
Node()
{
this->data = 0;
next = NULL;
}
Node(int data)
{
this->data = data;
this->next = NULL;
}
Node(int data, Node* next)
{
this->data = data;
this->next = next;
}
};
*/
Node* sortList(Node *head){
// Write your code here.
Node* z=new Node(-1),*z_=z;
Node* o=new Node(-1),*o_=o;
Node* t=new Node(-1),*t_=t;
Node* temp=head;
while(temp){
if(temp->data==0) {
z_->next=temp;
z_=z_->next;
}
else if(temp->data==1){
o_->next=temp;
o_=o_->next;
} else {
t_->next = temp;
t_=t_->next;
}
temp=temp->next;
}
z_->next=o->next?o->next : t->next;
o_->next=t->next;
t_->next = NULL;
return z->next;
}
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
unordered_map<ListNode*, bool> mp;
ListNode *tempA = headA, *tempB = headB;
while (tempA) {
mp[tempA] = true;
tempA = tempA->next;
}
while (tempB) {
// mp[tempB] = true;
if(mp.find(tempB)!=mp.end()){
return tempB;
}
tempB = tempB->next;
}
return nullptr;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* tempA=headA;
while(tempA){
ListNode* tempB=headB;
while(tempB){
if(tempA==tempB) return tempA;
tempB=tempB->next;
}
tempA=tempA->next;
}
return nullptr;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* tempA=headA;
ListNode* tempB=headB;
while(tempA!=tempB){
tempA=tempA->next;
tempB=tempB->next;
if(tempA==tempB) return tempA;
if(!tempA) tempA=headB;
if(!tempB) tempB=headA;
}
return tempA;
}
};
/**
* Definition of linked list:
*
* class Node {
* public:
* int data;
* Node *next;
* Node() {
* this->data = 0;
* this->next = NULL;
* }
* Node(int data) {
* this->data = data;
* this->next = NULL;
* }
* Node (int data, Node *next) {
* this->data = data;
* this->next = next;
* }
* };
*
*************************************************************************/
int recursion(Node* head){
if(head==nullptr){
return 1;
}
int carry=recursion(head->next);
head->data=head->data+carry;
if(head->data<10){
return 0;
}
int x=head->data;
head->data=x%10;
x/=10;
return x;
}
Node *addOne(Node *head)
{
// Write Your Code Here.
Node* temp=head;
int carry=recursion(temp);
if(carry){
Node* newNode=new Node(carry);
newNode->next=head;
head=newNode;
}
return head;
}
/**
* Definition of linked list:
*
* class Node {
* public:
* int data;
* Node *next;
* Node() {
* this->data = 0;
* this->next = NULL;
* }
* Node(int data) {
* this->data = data;
* this->next = NULL;
* }
* Node (int data, Node *next) {
* this->data = data;
* this->next = next;
* }
* };
*
*************************************************************************/
Node *addOne(Node *head)
{
// Write Your Code Here.
vector<int> v;
while(head){
v.push_back(head->data);
head=head->next;
}
reverse(begin(v),end(v));
int curr=1;
for(int i=0;i<v.size();i++){
curr+=v[i];
v[i]=curr%10;
curr=(curr/10);
}
if(curr>0) v.push_back(curr);
reverse(begin(v),end(v));
for(int i=0;i<v.size();i++){
cout<<v[i]<<" ";
}
return head;
}