0% found this document useful (0 votes)
20 views

Lecture 14

The document discusses deleting nodes from a linked list. It describes deleting from the beginning by setting the head pointer to the next node and deleting the old head. It describes deleting from the end by traversing the list with two pointers until the second reaches null, then setting the first's next to null and deleting the second. Deletion from the middle is also listed but not explained in detail.

Uploaded by

burgerpizza326
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 14

The document discusses deleting nodes from a linked list. It describes deleting from the beginning by setting the head pointer to the next node and deleting the old head. It describes deleting from the end by traversing the list with two pointers until the second reaches null, then setting the first's next to null and deleting the second. Deletion from the middle is also listed but not explained in detail.

Uploaded by

burgerpizza326
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Data Structure & Algorithms

Maryam Imtiaz Malik


[email protected]
Delete Node from Linked List

 Deletion from Beginning


 Deletion from End
 Deletion from Middle
Delete from Beginning

250 4 101 5 200 2

head 250 101 200


250 4 101 5 200 2 270 67

head 250 101 200 270


Delete from Beginning

 node* ptr = head;

ptr

250 4 101 5 200 2

head 250 101 200


Delete from Beginning

 node* ptr = head;


 head = head -> link;

ptr

4 101 5 200 2
250 101 200

101

head
Delete from Beginning

 node* ptr = head;


 head = head -> link;
 delete ptr;

ptr

4 101 5 200 2
250 101 200

101

head
Delete from Beginning

 node* ptr = head;


 head = head -> link;
 delete ptr;

101 5 200 2

head 101 200


Delete from Beginning

 void deleteBeg(){
 struct node  if( head == NULL)
  cout<<"Empty List“;
{
 else
 int data;
 {
 node *link;  node* ptr = head;
 };  head = head -> link;
 delete ptr;
 node * head = NULL;
 count--;
 }
 }
Delete from End

250 4 101 5 200 2

head 250 101 200


Delete from End

 node *ptr, *prev;

250 4 101 5 200 2

head 250 101 200


Delete from End

 Traverse the list using ptr & prev until ptr -> link is NULL
 prev = ptr;
 ptr = ptr -> link;

ptr

250 4 101 5 200 2

head 250 101 200


Delete from End

 Traverse the list using ptr & prev until ptr -> link is NULL
 prev = ptr;
 ptr = ptr -> link;

prev ptr

250 4 101 5 200 2

head 250 101 200


Delete from End

 Traverse the list using ptr & prev until ptr -> link is NULL
 prev = ptr;
 ptr = ptr -> link;

prev ptr

250 4 101 5 200 2

head 250 101 200


Delete from End

 Traverse the list using ptr & prev until ptr -> link is NULL
 prev = ptr;
 ptr = ptr -> link;

prev ptr

250 4 101 5 200 2

head 250 101 200


Delete from End

 Traverse the list using ptr & prev until ptr -> link is NULL
 prev = ptr;
 ptr = ptr -> link;

prev ptr

250 4 101 5 200 2

head 250 101 200


Delete from End

 prev -> link = NULL


 delete ptr;

prev ptr

250 4 101 5 200 2

head 250 101 200


Delete from End

 prev -> link = NULL


 delete ptr;

250 4 101 5

head 250 101

You might also like