Professional Documents
Culture Documents
This Is Tricky: Only Advance If No Deletion
This Is Tricky: Only Advance If No Deletion
stack <int> s;
while(slow != NULL){
s.push(slow->data);
slow = slow->ptr;
}
while(head != NULL ){
int i=s.top();
s.pop();
if(head -> data != i){
return false;
}
head=head->ptr;
}
return true;
}
next_next;
if (current == NULL)
return;
if (head == NULL)
return;
if (head->next != NULL)
{
if (head->data == head->next->data)
{
to_free = head->next;
head->next = head->next->next;
free(to_free);
removeDuplicates(head);
}
else
{
removeDuplicates(head->next);
}
}
}
if ((*head_ref)->data == x) {
a = head_ref;
}
else if ((*head_ref)->data == y) {
b = head_ref;
}
head_ref = &((*head_ref)->next);
}
if (a && b) {
swap(*a, *b);
swap(((*a)->next), ((*b)->next));
}
}
void reverse()
{
Node* current = head;
Node *prev = NULL, *next = NULL;
12. Merge two sorted linked lists such that merged list is
in reverse order
return head;
}
deleteNodesOnRightSide(Node head){
if(head == null || head.next == null) return head;
Node nextNode = deleteNodesOnRightSide(head.next);
if(nextNode.data > head.data) return nextNode;
head.next = nextNode;
return head;
}
22. Rearrange a linked list such that all even and odd
positioned nodes are together
even->next = odd->next;
even = odd->next;
}
return head;
}
23. Rearrange a Linked List in Zig-Zag fashion
void zigZagList(L)
{
bool flag = true;
current = L;
while (L^.pas != NULL) {
if (flag)
{
if (current^.data > current^.next^.data) {
temp=current^.data;
current^.data = current^.next^.data
current^.next^.data=temp;
}
}
else
{
if (current^.data < current^.next^.data) {
temp=current^.data;
current^.data = current^.next^.data
current^.next^.data=temp;
}
}
current = current^.next;
flag = !flag;
}
}
return head;
}
33. Find the sum of last n nodes of the given Linked List
34. Count pairs from two linked lists whose sum is equal to
a given value
*q = q_curr; }
40. Insert a node after the n-th node from the end