can anyone pls go through the code and tell me why i am getting error while sunmitting
its working fine in other compilers
Error in Reversal of LL
@dare_devil_007 hi bro,apka logic sahi nhi hai is question me ,actually hme krna kya hi ll har k elemebts pr jakr reverse krna hai mtlb:pehle k elements of ll reverse kie fir agle k and so on ,also remember to do k=k%length of ll.Try it if any doubt then ask me.
#include<bits/stdc++.h>
using namespace std;
class node {
public:
int data;
node* next;
node(int d)
{
data = d;
next = NULL;
}
};
node* reverseList(node* head, int k)
{
int cnt = 1;
node* start = head;
node* h = NULL;
node* ptr = head;
while (cnt != k)
{
ptr = ptr->next;
cnt++;
}
h = ptr;
node* x = ptr;
while (k--)
{
node* t1 = start;
node* t2 = x->next;
x->next = start;
start = start->next;
t1->next = t2;
}
return h;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
int N;
int K;
cin >> N >> K;
int x;
node* head = NULL;
node* temp = NULL;
for (int i = 0; i < N; i++)
{
cin >> x;
if (head == NULL)
{
node* n = new node(x);
head = n; temp = n;
cout << head->data << " " << temp->data << endl;
}
else
{
node* n = new node(x);
temp->next = n;
temp = temp->next;
}
}
//node* re = reverseList(head, K);
node* re = reverseList(head, K);
while (re != NULL)
{
cout << re->data << "->";
re = re->next;
}
}
here is my code and its working fine other compilers
@dare_devil_007 hi apka code me koi error nhi hai but logic galat hai bhai ,ap khali pehli k nodes reverse krrhe ho hme every k nodes in ll kk reverse krna hai.
ok bhai,ab smjha.Thanks!!
@dare_devil_007welcm