Write a program to insert a node in a linked list.
Example 1:
Input: 5->2->8->4->1->NULL , value=12 // Insert node at beginning
Output: 12->5->2->8->4->1->NULL
Example 2:
Input: 5->2->8->4->1->NULL , value=12 // insert node at end
Output: 5->2->8->4->1->12->NULL
Example 3:
Input: 5->2->8->4->1->NULL , aftervalue=8, value=12 // insert after given node
Output: 5->2->8->12->4->1->NULL
Approach 1: Insert the node at the beginning.
Java
public class LinkListInsertion {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);int val = 12;ListNode node = insertAtBeginning(l, val);System.out.println("After adding :");linkedListTraversal(node);}private static void linkedListTraversal(ListNode node) {while (node != null) {System.out.print(node.val + " ");node = node.next;}}public static ListNode insertAtBeginning(ListNode head, int val) {ListNode newNode = new ListNode(val);newNode.next = head;head = newNode;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(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;}};//funtion to add the node//at beginning of the linked listNode *insertAtBeginning(Node *head,int value){//if list is the list is empty//then the new node is the only//node in listif(head==NULL){//create a new NodeNode *newNode=new Node(value);//point head to the//new Nodehead=newNode;//return headreturn head;}else{//create a new nodeNode *newNode = new Node(value);//point newNode->next to headnewNode->next=head;//point head to the newnodehead=newNode;//retrun 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 inert\n";linkedListTraversal(head);cout<<"\n";int value=12;head=insertAtBeginning(head,value);cout<<"List after inserting at beginning is\n";linkedListTraversal(head);return 0;}//Time Complexity: O(1)//Space Complexity: O(1)
Approach 2: Insert at the end of the linked list.
Java
public class LinkListInsertion {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);int val = 12;ListNode node = insertAtEnd(l, val);System.out.println("After adding :");linkedListTraversal(node);}private static void linkedListTraversal(ListNode node) {while (node != null) {System.out.print(node.val + " ");node = node.next;}}// Insert element at end of link listpublic static ListNode insertAtEnd(ListNode head, int val) {// create a new NodeListNode newNode = new ListNode(val);ListNode tmp = head;// if list is the list is empty// then the new node is the only// node in listif (head == null) {// point head to the// new Nodehead = newNode;return head;}// iterate till we reach at last// node of the linked listwhile (tmp.next != null) {// move head to nexttmp = tmp.next;}tmp.next = newNode;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;}};//funtion to add the node//at end of the linked listNode *insertAtEnd(Node *head,int value){//if list is the list is empty//then the new node is the only//node in listif(head==NULL){//create a new NodeNode *newNode=new Node(value);//point head to the//new Nodehead=newNode;//return headreturn head;}else{Node *temp=head;//iterate till we reach at last//node of the linked listwhile(temp->next!=NULL){//move to the next nodetemp=temp->next;}//create a new nodeNode *newNode=new Node(value);//point the last node to//the new nodetemp->next=newNode;//return the head of the//linked listreturn 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 inert\n";linkedListTraversal(head);cout<<"\n";int value=12;head=insertAtEnd(head,value);cout<<"Linked list after inserting at end is\n";linkedListTraversal(head);return 0;}//Time Complexity: O(n)//Space Complexity: O(1)
Approach 3: Given the node after which we need to insert the new node.
Java
public class LinkListInsertion {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);int aftervalue = 2, value = 12;ListNode node = insertAfterGivenNode(l, aftervalue, value);System.out.println("After adding :");linkedListTraversal(node);}private static void linkedListTraversal(ListNode node) {while (node != null) {System.out.print(node.val + " ");node = node.next;}}private static ListNode insertAfterGivenNode(ListNode head, int aftervalue, int value) {// Create new nodeListNode newNode = new ListNode(value);ListNode tmp = head;if (head == null) {// assign new node to headhead = newNode;return head;}// iterate till we not found// give node in the given linked listint check = 0;while (tmp.next != null) {if (tmp.val == aftervalue) {check++;break;}tmp = tmp.next;}// If node found then insert afterif (tmp != null && check == 1) {ListNode next = tmp.next;tmp.next = newNode;tmp.next.next = next;}// If node not found// Then insert in endif (check == 0)tmp.next = newNode;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;}};//funtion to add the node//at end of the linked listNode *insertAtEnd(Node *head,int value){//if list is the list is empty//then the new node is the only//node in listif(head==NULL){//create a new NodeNode *newNode=new Node(value);//point head to the//new Nodehead=newNode;//return headreturn head;}else{Node *temp=head;//iterarte till we reach at last//node of the linked listwhile(temp->next!=NULL){//move to the next nodetemp=temp->next;}//create a new nodeNode *newNode=new Node(value);//point the last node to//the new nodetemp->next=newNode;//return the head of the//linked listreturn head;}}//function to insert the node after//given nodeNode *insertAfterGivenNode(Node *head,int aftervalue,int value){if(head==NULL){//if head is null//then insert at the end of//the linked listhead=insertAtEnd(head,value);//return head of the linked listreturn head;}Node *temp=head;while(temp!=NULL){//if current head data//is same as the aftervalue//then breakif(temp->data==aftervalue){break;}//move to the next nodeelse{temp=temp->next;}}//if node is not find//then insert at the end//of the linked listif(temp==NULL){head=insertAtEnd(head,value);//return the head of//the linked listreturn head;}else{//create the new nodeNode *newNode=new Node(value);//insert the node after given nodenewNode->next=temp->next;temp->next=newNode;//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 insert is\n";linkedListTraversal(head);cout<<"\n";int aftervalue=8,value=12;head=insertAfterGivenNode(head,aftervalue,value);cout<<"Linked list after insert is \n";linkedListTraversal(head);return 0;}//Time Complexity: O(n)//Space Complexity: O(1)
No comments:
Post a Comment