Write a program to delete a node from the linked list.
Example 1:
Input: 5->2->8->4->1->NULL // delete node at beginning
Output: 2->8->4->1->NULL
Example 2:
Input: 5->2->8->4->1->NULL // delete node at end
Output: 5->2->8->4->NULL
Example 3:
Input: 5->2->8->4->1->NULL ,value // delete given node
Output: 5->2->4->1->NULL
Approach 1: Delete the node from the beginning.
Java
public class LinkListDeletion {public static void main(String[] args) {ListNode l = new ListNode(1);l.next = new ListNode(2);l.next.next = new ListNode(3);l.next.next.next = new ListNode(4);l.next.next.next.next = new ListNode(5);ListNode node = deletionAtBeginning(l);System.out.println("After deleting :");linkedListTraversal(node);}private static void linkedListTraversal(ListNode node) {while (node != null) {System.out.print(node.val + " ");node = node.next;}}public static ListNode deletionAtBeginning(ListNode head) {// If list is nullif (head == null) {return head;}return head.next;}}class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}}// Time Complexity : O(1)// Space Complexity : O(1)
C++
#include <bits/stdc++.h>using namespace std;//Struture of nodestruct Node{int data;Node *next;Node(int data){this->data=data;this->next=NULL;}};//function to delete the//first node from the linked//listNode *deleteAtBeginning(Node *head){//if linked list is//empty return NULLif(head==NULL)return NULL;//store the first elemntNode *temp=head;//head=head->next;//delete the first nodefree(temp);return head;}//function to traverse(print) a linked//listvoid linkedListTraversal(Node *head){Node *temp=head;//iterate till we reach end of the linked//listwhile(temp!=NULL){//print the current//node datacout<<temp->data<<" ";//move to next nodetemp=temp->next;}}int main(){Node *head=new Node(5);head->next=new Node(2);head->next->next=new Node(8);head->next->next->next=new Node(4);head->next->next->next->next=new Node(1);cout<<"Linked list befor delete is\n";linkedListTraversal(head);cout<<"\n";head=deleteAtBeginning(head);cout<<"Linked list after delete from beginning is \n";linkedListTraversal(head);return 0;}//Time Complexity: O(1)//Space Complexity: O(1)
Approach 2:Delete node from the end of the linked list.
Java
public class LinkListDeletion {public static void main(String[] args) {ListNode l = new ListNode(5);l.next = new ListNode(2);l.next.next = new ListNode(8);l.next.next.next = new ListNode(4);l.next.next.next.next = new ListNode(1);ListNode node = deletionAtEnd(l);System.out.println("After deleting :");linkedListTraversal(node);}private static void linkedListTraversal(ListNode node) {while (node != null) {System.out.print(node.val + " ");node = node.next;}}public static ListNode deletionAtEnd(ListNode head) {ListNode tmp = head;// If list is blank or single nodeif (head == null || head.next == null) {return null;}// Iterate till tmp.next->next is nullwhile (tmp.next.next != null) {tmp = tmp.next;}tmp.next = null;return head;}}class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}}// Time Complexity : O(n)// Space Complexity : O(1)
C++
#include <bits/stdc++.h>using namespace std;//Struture of nodestruct Node{int data;Node *next;Node(int data){this->data=data;this->next=NULL;}};//function to delete the//last node from the linked listNode *deleteAtEnd(Node *head){//if linked list is//empty or a single node return NULLif(head==NULL||head->next==NULL)return NULL;Node *temp=head,*prev=head;//iterate till we reach//at the end of listwhile(temp->next!=NULL){//store the previous//of the nodeprev=temp;temp=temp->next;}prev->next=NULL;free(temp);return head;}//function to traverse(print) a linked//listvoid linkedListTraversal(Node *head){Node *temp=head;//iterate till we reach end of the linked//listwhile(temp!=NULL){//print the current//node datacout<<temp->data<<" ";//move to next nodetemp=temp->next;}}int main(){Node *head=new Node(5);head->next=new Node(2);head->next->next=new Node(8);head->next->next->next=new Node(4);head->next->next->next->next=new Node(1);cout<<"Linked list befor delete is\n";linkedListTraversal(head);cout<<"\n";head=deleteAtEnd(head);cout<<"Linked list after delete from end is \n";linkedListTraversal(head);return 0;}//Time Complexity: O(n)//Space Complexity: O(1)
Approach 3: Delete the given node from the linked list.
Java
public class LinkListDeletion {public static void main(String[] args) {ListNode l = new ListNode(5);l.next = new ListNode(2);l.next.next = new ListNode(8);l.next.next.next = new ListNode(4);l.next.next.next.next = new ListNode(1);int value = 8;ListNode node = deleteGivenNode(l, value);System.out.println("After deleting :");linkedListTraversal(node);}private static void linkedListTraversal(ListNode node) {while (node != null) {System.out.print(node.val + " ");node = node.next;}}public static ListNode deleteGivenNode(ListNode head, int value) {ListNode tmp = head;// If list is blank or single nodeif (head == null) {return null;}// If head equal to deleting valueif (head.val == value) {return head.next;}// Iterate till tmp.next->next is nullListNode pre = head;while (tmp != null) {if (tmp.val == value) {break;}pre = tmp;tmp = tmp.next;}if (tmp != null) {pre.next = tmp.next;}return head;}}class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}}// Time Complexity : O(n)// Space Complexity : O(1)
C++
#include <bits/stdc++.h>using namespace std;//Struture of nodestruct Node{int data;Node *next;Node(int data){this->data=data;this->next=NULL;}};//function to delete the//given node from the linked listNode *deleteGivenNode(Node *head,int value){//if linked list is//empty return NULLif(head==NULL)return NULL;if(head->data==value){head=head->next;return head;}Node *temp=head,*prev=head;//iterate till we reach//at the end of listwhile(temp!=NULL){//if we found the node// then breakif(temp->data==value){break;}//else move to the next nodeelse{prev=temp;temp=temp->next;}}//if node is not found//in the list then just return//the original listif(temp==NULL)return head;//delete the the given nodeelse{prev->next=temp->next;//free the given nodefree(temp);//return the headreturn head;}}//function to traverse(print) a linked//listvoid linkedListTraversal(Node *head){Node *temp=head;//iterate till we reach end of the linked//listwhile(temp!=NULL){//print the current//node datacout<<temp->data<<" ";//move to next nodetemp=temp->next;}}int main(){Node *head=new Node(5);head->next=new Node(2);head->next->next=new Node(8);head->next->next->next=new Node(4);head->next->next->next->next=new Node(1);cout<<"Linked list befor delete is\n";linkedListTraversal(head);cout<<"\n";int value=8;head=deleteGivenNode(head,value);cout<<"Linked list after delete given node is \n";linkedListTraversal(head);return 0;}//Time Complexity: O(n)//Space Complexity: O(1)
No comments:
Post a Comment