Skip to content

Commit 6259062

Browse files
authored
Merge pull request #996 from AnantB1/main
Absolute List Sorting.cpp
2 parents 0ad633c + 677e4cd commit 6259062

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Linked List Node
5+
struct Node
6+
{
7+
Node* next;
8+
int data;
9+
};
10+
void sortList(Node** head);
11+
// Utility function to insert a node at the
12+
// beginning
13+
void push(Node** head, int data)
14+
{
15+
Node* newNode = new Node;
16+
newNode->next = (*head);
17+
newNode->data = data;
18+
(*head) = newNode;
19+
}
20+
21+
// Utility function to print a linked list
22+
void printList(Node* head)
23+
{
24+
while (head != NULL)
25+
{
26+
cout << head->data;
27+
if (head->next != NULL)
28+
cout << " ";
29+
head = head->next;
30+
}
31+
cout<<endl;
32+
}
33+
34+
35+
// Driver code
36+
int main()
37+
{
38+
39+
int t=0;
40+
int n = 0;
41+
cin>>t;
42+
while(t--)
43+
{
44+
Node* head = NULL;
45+
cin>>n;
46+
int arr[n];
47+
for(int i=0;i<n;i++)
48+
{
49+
cin>>arr[i];
50+
// push(&head, a);
51+
}
52+
for(int i=n-1;i>=0;i--)
53+
{
54+
push(&head, arr[i]);
55+
}
56+
// printList(head);
57+
58+
sortList(&head);
59+
60+
printList(head);
61+
62+
}
63+
return 0;
64+
}
65+
// } Driver Code Ends
66+
67+
68+
/* The structure of the Linked list Node is as follows:
69+
struct Node
70+
{
71+
Node* next;
72+
int data;
73+
}; */
74+
75+
/*Your method shouldn't print anything
76+
it should transform the passed linked list */
77+
void sortList(Node** head)
78+
{
79+
// Your Code Here
80+
Node *cur=*head,*temp;
81+
82+
while(cur!=NULL && cur->next!=NULL)
83+
{
84+
if(cur->next->data < 0)
85+
{
86+
temp=cur->next;
87+
cur->next=temp->next;
88+
temp->next=*head;
89+
*head=temp;
90+
}
91+
else
92+
{
93+
cur=cur->next;
94+
}
95+
96+
}
97+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <iostream>
5+
#include <stack>
6+
using namespace std;
7+
/* Link list Node */
8+
struct Node {
9+
int data;
10+
struct Node *next;
11+
Node(int x) {
12+
data = x;
13+
next = NULL;
14+
}
15+
};
16+
17+
18+
19+
bool isPalindrome(Node *head);
20+
/* Driver program to test above function*/
21+
int main()
22+
{
23+
int T,i,n,l,firstdata;
24+
cin>>T;
25+
while(T--)
26+
{
27+
28+
struct Node *head = NULL, *tail = NULL;
29+
cin>>n;
30+
// taking first data of LL
31+
cin>>firstdata;
32+
head = new Node(firstdata);
33+
tail = head;
34+
// taking remaining data of LL
35+
for(i=1;i<n;i++)
36+
{
37+
cin>>l;
38+
tail->next = new Node(l);
39+
tail = tail->next;
40+
}
41+
cout<<isPalindrome(head)<<endl;
42+
}
43+
return 0;
44+
}
45+
46+
// } Driver Code Ends
47+
48+
49+
/*
50+
struct Node {
51+
int data;
52+
struct Node *next;
53+
Node(int x) {
54+
data = x;
55+
next = NULL;
56+
}
57+
};
58+
*/
59+
/*You are required to complete this method */
60+
bool isPalindrome(Node *head)
61+
{
62+
//Your code here
63+
deque <int> q;
64+
Node *cur=head;
65+
while(cur!=NULL)
66+
{
67+
q.push_back(cur->data);
68+
cur=cur->next;
69+
}
70+
71+
while(q.size() >1)
72+
{
73+
if(q.front()==q.back())
74+
{
75+
q.pop_front();
76+
q.pop_back();
77+
}
78+
else
79+
{
80+
return false;
81+
}
82+
83+
}
84+
return true;
85+
86+
}

0 commit comments

Comments
 (0)