Relative Content

Tag Archive for c++linked-list

LeetCode 328. Odd Even Linked List

class Solution { public: ListNode* oddEvenList(ListNode* head) { if(head==NULL || head->next==NULL){ return head; } ListNode* odd=head; ListNode* even=head->next; ListNode* evenHead=head->next; while(odd->next!=NULL && odd->next->next!=NULL){ odd->next=odd->next->next; odd=odd->next; } while(even!=NULL && even->next!=NULL){ even->next=even->next->next; even=even->next; } odd->next=evenHead; return head; } }; Error: AddressSanitizer: heap-use-after-free on address {1,2,3,4,5,6,7,8}================>{1,3,3,5,7,2,4,6} c++ linked-list

generate number from reverse linked list

I have create a function int reverseNum(Node *head) to get number from reverse linked list.
eg. for Linked List:- 1->2->3->4 the number will be 4321.
But the program is returning 4320.

Segfault while calculating singly linked list’s size in C++

int List::size() { // FIXME: implement this method to return the number of Nodes in the list int listSize = 0; Node* temp = first; while (temp != nullptr) { listSize++; temp = temp->next; } return listSize; } I’m implementing this linked list class method to calculate the number of nodes in the list. When […]

Insert a node at the end of the linked list

I was trying to insert a node at the end of the linked list. It could be compiled and run in the right way but it didn’t print out anything. Is it wrong with the `Insert(int data)’or ‘Print()’?
Here is my code.

QuickSort Linked List in C

I need to use the Quicksort sorting algorithm to sort elements of a linked list. It is important to note that I must not just swap the key values, but the elements themselves. My code simply does not work; I believe I am making a mistake with the recursion. I would like a solution.