Trying to solve Odd Even Linked List question.
Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list.
My try:
/**
* 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 == nullptr || head->next == nullptr) return head;
int n = 1;
ListNode* l = nullptr;
ListNode *u = l;
ListNode* r = r;
ListNode* ru = nullptr;
while(head){
ListNode* c = new ListNode(head->val);
if(n%2){
if(r == nullptr){
r = c;
}
else{
r->next = c;
r = r->next;
}
}
else{
if(l == nullptr){
l = c;
}
else{
l->next = c;
l = l->next;
}
}
n++;
head=head->next;
}
l->next = ru;
return u;
}
};
But getting the below error:
Line 27: Char 24: runtime error: member access within misaligned address 0x9ddfea08eb382d69 for type 'ListNode', which requires 8 byte alignment (solution.cpp)
0x9ddfea08eb382d69: note: pointer points here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:36:24
What does the error mean and to solve it.
Link: https://leetcode.com/problems/odd-even-linked-list/
The problem is (once we fix the typo):
ListNode *l = nullptr;
ListNode *u = l;
...
return u;
u is always nullptr. One quick hack would be:
ListNode *l = nullptr;
ListNode **u = &l;
ListNode *r = nullptr;
ListNode **ru = &r;
...
l->next = *ru;
return *u;
Or you could try:
if(r == nullptr){
r = c;
ru = r; // Add this
}
...
if(l == nullptr){
l = c;
u = l; // Add this
}
Please check the code with comments marked as // CHANGE HERE
Tested here: https://godbolt.org/z/c3e5M739d
#include <iostream>
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 == nullptr || head->next == nullptr) return head;
int n = 1;
// CHANGE HERE: set all to nullptr
ListNode* l = nullptr;
ListNode* u = nullptr;
ListNode* r = nullptr;
ListNode* ru = nullptr;
while(head){
// std::cout << head->val << " ";
ListNode* c = new ListNode(head->val);
if(n%2){
// CHANGE HERE: replace r with ru
if(ru == nullptr){
r = c;
// CHANGE HERE: set ru
ru = r;
}
else{
r->next = c;
r = r->next;
}
}
else{
// CHANGE HERE: replace l with u
if(u == nullptr){
l = c;
// CHANGE HERE: set u
u = l;
}
else{
l->next = c;
l = l->next;
}
}
n++;
head=head->next;
}
l->next = ru;
return u;
}
};
int main()
{
ListNode* node = new ListNode(1);
node->next = new ListNode(2);
node->next->next = new ListNode(3);
node->next->next->next = new ListNode(4);
node->next->next->next->next = new ListNode(5);
node->next->next->next->next->next = new ListNode(6);
Solution s;
ListNode* l = s.oddEvenList(node);
ListNode* q = l;
while (q)
{
std::cout << q->val << " ";
q = q->next;
}
}
Related
Hi I'm trying to write a code for singly linked list that reorders the nodes so that:
L1->L2->L3->...->Ln to L1->Ln->L2->Ln-1->L3->Ln-2...
So I tried to do this by finding the node at the end of the list and setting that node as the next of current node and then finishing the loop by setting the current node as the next next of current node.
#include <cstdlib>
#include <iostream>
using namespace std;
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) {}
};
ListNode* newNode(int x){
ListNode* temp = new ListNode;
temp->val = x;
temp->next = NULL;
return temp;
}
void printlist(ListNode* head)
{
while (head != NULL) {
cout << head->val << " ";
if (head->next)
cout << "-> ";
head = head->next;
}
cout << endl;
}
void reorderList(ListNode* head){
ListNode *curr = head;
ListNode *temp=head;
ListNode *last=NULL;
while(curr->next != NULL){
while(temp->next != NULL){
temp=temp->next;
last=temp;
}
curr->next=last;
last->next=curr->next->next;
curr=curr->next->next;
temp=curr->next;
}
}
int main(int argc, char** argv) {
ListNode* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
printlist(head); // Print original list
reorderList(head); // Modify the list
printlist(head); // Print modified list
return 0;
}
So far, after displaying the original list, the program stops by saying that the run failed. I'm having some problem understanding the singly linked list and I don't know what I'm doing wrong.
I have modified your code to show a correct answer:
#include <iostream>
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 ) {}
};
void printlist( ListNode* head )
{
while( head != nullptr ) {
std::cout << head->val << " ";
if( head->next )
std::cout << "-> ";
head = head->next;
}
std::cout << std::endl;
}
void reorderList( ListNode* head ) {
ListNode* pf = head;
ListNode* pt = nullptr;
ListNode* tmp = nullptr;
while( true )
{
// find n-1 node
tmp = pf;
while( tmp && tmp->next && tmp->next->next )
tmp = tmp->next;
// check to see n-1 node is not equal to the first node
if( tmp == pf )
break;
// reorder
pt = tmp;
tmp = pf->next;
pf->next = pt->next;
pt->next->next = tmp;
pf = tmp;
pt->next = nullptr;
}
}
int main( int argc, char** argv ) {
ListNode* head = new ListNode( 1 );
head->next = new ListNode( 2 );
head->next->next = new ListNode( 3 );
head->next->next->next = new ListNode( 4 );
head->next->next->next->next = new ListNode( 5 );
head->next->next->next->next->next = new ListNode( 6 );
printlist( head ); // Print original list
reorderList( head ); // Modify the list
printlist( head ); // Print modified list
return 0;
}
and the result is like below:
1 -> 2 -> 3 -> 4 -> 5 -> 6
1 -> 6 -> 2 -> 5 -> 3 -> 4
There could be many solutions to your problem. I just modified your logic and added comments which could help you to understand. Happy link list coding.
#include <cstdlib>
#include <iostream>
using namespace std;
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) {}
};
ListNode* newNode(int x){
ListNode* temp = new ListNode;
temp->val = x;
temp->next = NULL;
return temp;
}
void printlist(ListNode* head)
{
while (head != NULL) {
cout << head->val << " ";
if (head->next)
cout << "-> ";
head = head->next;
}
cout << endl;
}
void reorderList(ListNode* head){
ListNode *curr = head;
ListNode *temp=head;
ListNode *last=NULL,*prev;
while(curr->next != NULL){
while(temp->next != NULL){
//Prev variable is being used for remove last node connection from it previous node
// For example 1->2->3
// We need to remove 2->3 connection before adding 1->3
// Otherwise it will create cycle i.e 1->3->2->3->2....
prev = temp;
temp=temp->next;
last=temp;
}
// If node number is even this condition will check adding mid+1 node twice
if(last==NULL) {
break;
}
temp = curr->next;
curr->next=last;
last->next=temp;
curr=curr->next->next;
temp=curr->next;
// Removing last node connection from it previous node
prev->next = NULL;
last = NULL;
}
}
int main(int argc, char** argv) {
ListNode* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
head->next->next->next->next = newNode(5);
//head->next->next->next->next->next = newNode(8);
printlist(head); // Print original list
reorderList(head); // Modify the list
printlist(head); // Print modified list
return 0;
}
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* temp2 = head;
ListNode* temp1 = head;
ListNode* temp = head;
while (temp != NULL)
{
++count;
temp = temp->next;
}
int i=1;
while(i!=(n-count) && temp1->next!=NULL && temp2->next!=NULL)
{
i++;
temp2=temp2->next;
temp1=temp1->next;
}
temp2=temp1->next; //move temp2 to next
temp2->val=temp1->val; //put val of temp2 to temp---so val erased
temp1->next=temp2->next; //adjust link
delete(temp2); //free
return head;
}
};
I got this error
member access within null pointer of type 'struct ListNode'
I did apply null checks, but I'm not able to detect the error. It's about "Remove Nth Node From End of List".
You need to rethink or clearly define what "nth" means.
What about the corner cases? Like -1 or 0 or count-1 or count or count+1.
After that, please add some validation of the parameter "n".
You have also one major problem in the code. You mixed up (count - n) with 0(n - count)
A potential solution with assumptions on what nth means could be:
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* temp2 = head;
ListNode* temp1 = head;
ListNode* temp = head;
while (temp != nullptr)
{
++count;
temp = temp->next;
}
if ((count > 0) && (n>0) && ((count - n) > 0)) {
int i = 1;
while (i != (count - n) && temp1->next != nullptr && temp2->next != nullptr)
{
i++;
temp2 = temp2->next;
temp1 = temp1->next;
}
temp2 = temp1->next; //move temp2 to next
//temp2->val = temp1->val; //put val of temp2 to temp---so val erased
temp1->next = temp2->next; //adjust link
delete temp2; //free
return head;
}
}
};
int main() {
// Build linked list
ListNode *n5 = new ListNode(5);
ListNode* n4 = new ListNode(4, n5);
ListNode* n3 = new ListNode(3, n4);
ListNode* n2 = new ListNode(2, n3);
ListNode* n1 = new ListNode(1, n2);
ListNode* n0 = new ListNode(0, n1);
Solution solution;
solution.removeNthFromEnd(n0,6);
}
This code add Two sum integer using linkedList.
I have added to the struct ListNode a new constructor in order to give him as input two vectore A and B.
ListNode(vector<int> array)
{
vector<int>::iterator itr = array.begin();
ListNode *t = nullptr;
for (; itr < array.end(); itr++) {
t->val = *itr;
t = t->next;
}
}
This is a strange overflow ??
#include <iostream>
#include <vector>
using namespace std;
/*
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) {}
ListNode(vector<int> array)
{
vector<int>::iterator itr = array.begin();
ListNode *t = nullptr;
for (; itr < array.end(); itr++) {
t->val = *itr;
t = t->next;
}
}
};
class Solution {
public:
Solution(){};
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *p = l1;
ListNode *q = l2;
ListNode *dummyHead = new ListNode(0);
ListNode *current = dummyHead;
int carry = 0;
while (p != NULL || q != NULL) {
int x = (p != NULL) ? p->val : 0;
int y = (q != NULL) ? q->val : 0;
int sum = carry + x + y;
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current -> next;
if (p != NULL) {
p = p -> next;
}
if (q != NULL) {
q = q -> next;
}
}
if (carry > 0) {
current -> next = new ListNode(carry);
}
return dummyHead->next;
}
};
int main(int argc, const char * argv[]) {
Solution *sol = nullptr;
vector<int> A = {2,4,3};
vector<int> B = {5,6,4};
ListNode *list1= new ListNode(A);
ListNode *list2= new ListNode(B);
sol->addTwoNumbers(list1,list2);
return 0;
}
Use case for test:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Your ListNode constructor is not creating a new ListNode for t to point at before you access t->val. It is also not even attempting to initialize the members of this.
Try something more like this instead:
struct ListNode {
int val = 0;
ListNode *next = nullptr;
ListNode(int x = 0, ListNode *next = nullptr) : val(x), next(next) {}
ListNode(const vector<int> &array)
{
if (!array.empty()) {
auto itr = array.begin();
val = *itr++;
ListNode **t = &next;
while (itr != array.end()) {
*t = new ListNode(*itr++);
t = &((*t)->next);
}
}
}
};
That being said, having such a constructor in ListNode really makes no sense. A list node should only care about its own data, not about the data of the nodes around it. This kind of list construction really belongs in a separate List class that wraps a chain of ListNodes, eg:
#include <iostream>
#include <vector>
#include <utility>
/*
Definition for singly-linked list.
*/
struct ListNode {
int val = 0;
ListNode *next = nullptr;
ListNode(int x = 0, ListNode *next = nullptr) : val(x), next(next) {}
};
class List {
private:
ListNode *head = nullptr;
public:
List() = default;
List(const vector<int> &vec)
{
ListNode **t = &head;
for (int val : vec) {
*t = new ListNode(val);
t = &((*t)->next);
}
}
List addNumber(const List &l) const {
ListNode *p = head;
ListNode *q = l.head;
List dummyList;
ListNode** current = &(dummyList.head);
int carry = 0;
while (p || q) {
int x = (p) ? p->val : 0;
int y = (q) ? q->val : 0;
int sum = carry + x + y;
carry = sum / 10;
*current = new ListNode(sum % 10);
current = &((*current)->next);
if (p) {
p = p->next;
}
if (q) {
q = q->next;
}
}
if (carry > 0) {
*current = new ListNode(carry);
}
return dummyList;
}
// what follows is stuff needed for compliance with the "Rule of 3/5/0":
// https://en.cppreference.com/w/cpp/language/rule_of_three
List(const List &list)
{
ListNode **t = &head;
for(ListNode *n = list.head; n; n = n->next) {
*t = new ListNode(n->val);
t = &((*t)->next);
}
}
List(List &&list)
{
std::swap(head, list.head);
}
~List()
{
ListNode *n;
for (ListNode *t = head; t; t = n) {
n = t->next;
delete t;
}
}
List& operator=(List rhs) {
List temp(std::move(rhs));
std::swap(head, temp.head);
return *this;
}
};
class Solution {
public:
List addTwoNumbers(const List &l1, const List &l2) {
return l1.addNumber(l2);
}
};
int main(int argc, const char * argv[]) {
Solution sol;
vector<int> A = {2,4,3};
vector<int> B = {5,6,4};
List list1(A);
List list2(B);
List result = sol.addTwoNumbers(list1, list2);
return 0;
}
You are dereferencing a null pointer that's why you get the memory error. You have to initialize it and allocate memory. Here you initialize ListNode *t = nullptr;. So t is referencing as NULL pointer. Next, you here dereferencing t->val = *itr; but t is pointing to NULL.
Try something like this for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
};
Then your main task is to create a link list from a vector, for this you can use a class that holds the vector link list. And another requirement is to add to the vector link list. So we can create a vector link list in reverse order. And for this, you can use this code snippet.
class VectorList {
public:
ListNode *root;
VectorList() : root(nullptr){}
VectorList(vector<int> &array){
root = new ListNode();
ListNode *curr;
curr = root;
int _size = array.size();
for(int i = _size - 1; i >= 0; i--){
ListNode *t = new ListNode(array[i]);
curr->next = t;
curr = curr->next;
}
root = root->next;
}
};
So now we can create two vector lists and then add the vector lists and print the desired answer. And for this, you can use this code snippet.
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* curr, *root = NULL;
int carry = 0;
while(l1 || l2){
if(l1 != NULL && l2 != NULL){
ListNode* t = new ListNode;
t->val = (l1->val + l2->val + carry) % 10;
t->next = NULL;
carry = (l1->val + l2->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l1 = l1->next;
l2 = l2->next;
}
else if(l1 == NULL){
ListNode* t = new ListNode;
t->val = (l2->val + carry) % 10;
t->next = NULL;
carry = (l2->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l2 = l2->next;
}
else if(l2 == NULL){
ListNode* t = new ListNode;
t->val = (l1->val + carry) % 10;
t->next = NULL;
carry = (l1->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l1 = l1->next;
}
}
if(carry){
ListNode* t = new ListNode;
t->val = carry;
t->next = NULL;
curr->next = t;
}
return root;
}
void print(ListNode *root){
ListNode *curr = root;
while(curr){
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
};
So overall, here is the main code with the desired output.
#include <iostream>
#include <vector>
using namespace std;
/*
Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
};
class VectorList {
public:
ListNode *root;
VectorList() : root(nullptr){}
VectorList(vector<int> &array){
root = new ListNode();
ListNode *curr;
curr = root;
int _size = array.size();
for(int i = _size - 1; i >= 0; i--){
ListNode *t = new ListNode(array[i]);
curr->next = t;
curr = curr->next;
}
root = root->next;
}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* curr, *root = NULL;
int carry = 0;
while(l1 || l2){
if(l1 != NULL && l2 != NULL){
ListNode* t = new ListNode;
t->val = (l1->val + l2->val + carry) % 10;
t->next = NULL;
carry = (l1->val + l2->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l1 = l1->next;
l2 = l2->next;
}
else if(l1 == NULL){
ListNode* t = new ListNode;
t->val = (l2->val + carry) % 10;
t->next = NULL;
carry = (l2->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l2 = l2->next;
}
else if(l2 == NULL){
ListNode* t = new ListNode;
t->val = (l1->val + carry) % 10;
t->next = NULL;
carry = (l1->val + carry) / 10;
if(root == NULL){
curr = root = t;
}
else{
curr->next = t;
curr = curr->next;
}
l1 = l1->next;
}
}
if(carry){
ListNode* t = new ListNode;
t->val = carry;
t->next = NULL;
curr->next = t;
}
return root;
}
void print(ListNode *root){
ListNode *curr = root;
while(curr){
cout << curr->val << " ";
curr = curr->next;
}
cout << endl;
}
};
int main(int argc, const char * argv[]) {
Solution *sol = nullptr;
vector<int> A = {2,4,3};
vector<int> B = {5,6,4};
VectorList list1 = VectorList(A);
VectorList list2 = VectorList(B);
ListNode *root = sol->addTwoNumbers(list1.root, list2.root);
sol->print(root);
return 0;
}
Sample Input:
l1 = [2,4,3], l2 = [5,6,4]
Sample Output:
[7 0 8]
Suppose headA points to [1, 3, 5, 7, 9, 11] and headB points to [2, 4 ,9, 11]. I want to find the common intersecting element problem statement
I am not understanding why a_pointer and b_pointer is returning a null list in the end.
I followed this tutorial
Algorithm followed:(as below)
#include<bits/stdc++.h>
struct ListNode {
int val;
ListNode *next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode *head) { val = x; next = head; }
};
class Solution {
public:
void print(ListNode *head)
{
while(head != nullptr)
{
printf("%d ->", head->val );
head = head->next;
}
printf("\n");
}
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
print(headA);
print(headB);
ListNode *a_pointer, *b_pointer;
a_pointer = headA;
b_pointer = headB;
while(a_pointer != b_pointer)
{
if(a_pointer == nullptr)
{
a_pointer = headB;
}
else
{
a_pointer = a_pointer->next;
}
if(b_pointer == nullptr)
{
b_pointer = headA;
}
else
{
b_pointer = b_pointer->next;
}
}
print(a_pointer);
print(b_pointer);
return a_pointer;
}
};
int main()
{
Solution s;
ListNode *node1 = new ListNode(1);
node1->next = new ListNode(3);
node1->next->next = new ListNode(5);
node1->next->next->next = new ListNode(7);
node1->next->next->next->next = new ListNode(9);
node1->next->next->next->next->next = new ListNode(11);
ListNode *node2 = new ListNode(2);
node2->next = new ListNode(4);
node2->next->next = new ListNode(9);
node2->next->next->next = new ListNode(11);
ListNode *ret = s.getIntersectionNode(node1,node2);
}
I am not understanding why the assignment a_pointer = headB is changing the original headB pointer.
It is reasonable to not understand why original headB pointer would be changed, because the original headB pointer is not being changed.
https://leetcode.com/problems/reorder-list/
this is my codes
logic
1) taking care of edge cases when 0,1,2 elements in list
2) storing node addresses in vector addr
3) inserting last nodes after 1st nodes one by one i.e 1-2-3 should become 1-3-2
/**
* 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) {}
* };
*/
#define Node ListNode
class Solution {
public:
void reorderList(ListNode* head)
{
if(head == nullptr || head->next == nullptr || head->next->next == nullptr)
{
return;
}
// copy to vector
vector <Node*> addr;
Node *temp = head;
while(temp != nullptr)
{
addr.push_back(temp);
temp = temp->next;
}
int n = addr.size();
Node *ins = addr[--n];
temp = head;
while(temp != ins)
{
// insert after temp
ins->next = temp->next;
temp->next = ins;
if((--n) != 0)
ins = addr[n];
temp = temp->next->next;
}
temp->next = nullptr;
}
};
this is my output
Line 924: Char 34: runtime error: addition of unsigned offset to 0x603000000010 overflowed to 0x603000000008 (stl_vector.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_vector.h:933:34
random edits made and code worked altho i still dunno why the above code is not working!
class Solution {
public:
void reorderList(Node* head)
{
if(head == nullptr || head->next == nullptr || head->next->next == nullptr)
{
return;
}
// copy to vector
vector <Node*> addr;
Node *temp = head;
while(temp != nullptr)
{
addr.push_back(temp);
temp = temp->next;
}
int n = addr.size();
Node *ins = addr[--n];
temp = head;
while(temp != ins && temp->next != ins)
{
// insert after temp
ins->next = temp->next;
temp->next = ins;
ins = addr[--n];
temp = temp->next->next;
}
if(temp->next == ins)
{
ins->next = nullptr;
}
else
{
temp->next = nullptr;
}
}
};