Monday, December 12, 2011

Delete or Remove nodes in a Linked List

Question 0: Remove Duplicates in a Linked List
Question 0.1:
Remove DUPLICATES from
i)Sorted Linked List
http://www.programcreek.com/2013/01/leetcode-remove-duplicates-from-sorted-list/
http://www.programcreek.com/2014/06/leetcode-remove-duplicates-from-sorted-list-ii-java/
http://www.geeksforgeeks.org/remove-duplicates-from-a-sorted-linked-list/
ii)Unsorted Linked List
http://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list/

Question 0.2:
Given a linked list and a number, remove all the nodes in the linked list whose 'key' value is equal to the given number. For example, given the linked list 5->6->1->3->5->5->7->8 and number 5 as input to the function, the function should modify the linked list as 6->1->3->7->8.
http://www.programcreek.com/2014/04/leetcode-remove-linked-list-elements-java/

Question 1: DeleteList() that takes a list, deallocates all of its memory and sets its head pointer to NULL (the empty list).
http://www.geeksforgeeks.org/write-a-function-to-delete-a-linked-list/

Question 2: Delete alternate nodes in a linked list.
http://www.geeksforgeeks.org/delete-alternate-nodes-of-a-linked-list/

Question 3: Delete kth node from the end of the Linked List
http://www.geeksforgeeks.org/nth-node-from-the-end-of-a-linked-list/

Question 4: Delete a node in a singly linked list given only a pointer to the node to be deleted. What happens when LL is circular ?
http://www.geeksforgeeks.org/in-a-linked-list-given-only-a-pointer-to-a-node-to-be-deleted-in-a-singly-linked-list-how-do-you-delete-it/
http://www.geeksforgeeks.org/how-to-write-functions-that-modify-the-head-pointer-of-a-linked-list/
http://www.geeksforgeeks.org/delete-a-given-node-in-linked-list-under-given-constraints/

Question 5: Delete N nodes after M nodes of a linked list
Given a linked list and two integers M and N. Traverse the linked list such that you retain M nodes then delete next N nodes, continue the same till end of the linked list.
http://www.geeksforgeeks.org/delete-n-nodes-after-m-nodes-of-a-linked-list/
Question 6:  Delete middle of linked list
Given a singly linked list, delete middle of the linked list. For example, if given linked list is 1->2->3->4->5 then linked list should be modified to 1->2->4->5
http://www.geeksforgeeks.org/delete-middle-of-linked-list/
Question 7: Delete a node in singly linked list if it is less than any of the successor nodes.

Delete kth Node from End
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
struct node
{
int data;
struct node* next;
};

void printNthFromLast(struct node *head, int n)
{
struct node *main_ptr = head;
struct node *ref_ptr = head;
struct node *prev=NULL;
struct node *temp=NULL;

int count = 0;
if(head != NULL)
{
while( count < n )
{
if(ref_ptr == NULL)
{
printf("%d is greater than the no. of "
"nodes in list", n);
return;
}
ref_ptr = ref_ptr->next;
count++;
}

while(ref_ptr != NULL)
{ prev=main_ptr;
main_ptr = main_ptr->next;
ref_ptr = ref_ptr->next;
}
temp=main_ptr;
prev->next=temp->next;

printf("Node no. %d from last is %d ",
n, temp->data);

free(temp);
}
}

void push(struct node** head_ref, int new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));

new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;
}

void print(struct node *current)
{
while(current!=NULL)
{
printf(" %d --> ", current->data);
current=current->next;

}

}

int main()
{

struct node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 5);
push(&head, 20);

printNthFromLast(head, 3);

print(head);

getchar();
}


Question 3:
You can't delete the current node because then the pointer of the previous node which points to this node would be invalid. The data as well as the pointer of the next node are copied to the current node and the next node is deleted
void delete(struct node **n)
{
    struct node *temp,*t;
    temp=*n;
    //copy the data of next node to current node
    temp->data=temp->next->data;
    //point current node to next node and set the next pointer
    t=temp->next;
    temp->next=t->next;
    free(t);
}
Obviously this approach would fail if the node to be deleted is the last node of the list. To do a work around for this you could mark this node as a dummy or set some sort indication within the node and when you traverse the list from the beginning, store the information of the previous node ad delete the node if it is marked as unusable. 


Question 4:


Copy the data of next node into this and remove the next node.

No comments:

Post a Comment