Update adjacent nodes if the current node is zero in a Singly Linked List
Given a linked list. The task is to change the value of the previous and next node of a node to 0 if the current node is 0.
Examples:
Input : 2->3->4->5->0->9->0->9->NULL Output : 2->3->4->0->0->0->0->0->NULL Input : 0->2->3->4->0->0->NULL Output : 0->0->3->0->0->0->NULL
Algorithm:
- First step is to create two pointers prev and curr.
The prev will point to the previous node and curr to the current node. The reason for making use of prev is that we cannot backtrack in a singly linked list. - if the first node is 0 then check if the next node is 0
- If yes, then skip
- else change the next node to -1.
- Start iterating through the nodes of the list.
- if the current node is 0 and the variable temp is 0 then
- set the previous node to -1.
- set the temp variable to 1.
- if the temp value is 1 and current node data is not 1 set the current node to -1
- if the current node is 0 and the variable temp is 0 then
- Repeat step 3 until end of the list is reached.
- Traverse through the list and change the nodes with value -1 to 0.
Reason for making use of temp and -1 is that if we change the next node to 0 directly then this may lead to change the node after next node to 0 as a result of which the entire list may get changed to 0.
Below is the implementation of the above approach:
C++
// C++ program to update the adjacent // nodes in a linked list #include <bits/stdc++.h> using namespace std; // A linked list node struct Node { int data; struct Node* next; }; // Utility function to create a new Node Node* newNode(int data) { Node* temp = new Node; temp->data = data; temp->next = NULL; return temp; } // Function to display the linked list void printList(Node* node) { // iterate until end of the list is reached while (node != NULL) { cout << node->data << " "; node = node->next; } } // Function to udate the adjacent // nodes to zero void updateAdjacent(Node* head) { // Pointer to point to // the previous node Node* prev = head; // Pointer to the // current node Node* curr = head->next; // If the first node is zero and the // second node is not zero then change // the value to -1 if (prev->data == 0 && curr->data != 0) curr->data = -1; // Temp variable to denote if the // current node is zero then the next // node will be changed to zero int temp = 0; while (curr != NULL) { // if the temp vaiable is 1 and // current data is not zero if (temp == 1 && curr->data != 0) { // change the data to -1 curr->data = -1; temp = 0; } // if the data is zero if (curr->data == 0) { // set the temp variable to 1 temp = 1; // set the previous node data to -1 prev->data = -1; } curr = curr->next; prev = prev->next; } curr = head; // change all the nodes with -1 to 0 while (curr != NULL) { if (curr->data == -1) curr->data = 0; curr = curr->next; } } // Driver code int main() { // creating the linked list Node* head = newNode(2); head->next = newNode(3); head->next->next = newNode(4); head->next->next->next = newNode(5); head->next->next->next->next = newNode(0); head->next->next->next->next->next = newNode(9); head->next->next->next->next->next->next = newNode(0); head->next->next->next->next->next->next->next = newNode(9); updateAdjacent(head); printList(head); return 0; } |
Java
// Java program to update the adjacent // nodes in a linked list class GFG { // A linked list node static class Node { int data; Node next; } // Utility function to create a new Node static Node newNode(int data) { Node temp = new Node(); temp.data = data; temp.next = null; return temp; } // Function to display the linked list static void printList(Node node) { // iterate until end of the list is reached while (node != null) { System.out.print(node.data + " "); node = node.next; } } // Function to udate the adjacent // nodes to zero static void updateAdjacent(Node head) { // Pointer to point to // the previous node Node prev = head; // Pointer to the // current node Node curr = head.next; // If the first node is zero and the // second node is not zero then change // the value to -1 if (prev.data == 0 && curr.data != 0) curr.data = -1; // Temp variable to denote if the // current node is zero then the next // node will be changed to zero int temp = 0; while (curr != null) { // if the temp vaiable is 1 and // current data is not zero if (temp == 1 && curr.data != 0) { // change the data to -1 curr.data = -1; temp = 0; } // if the data is zero if (curr.data == 0) { // set the temp variable to 1 temp = 1; // set the previous node data to -1 prev.data = -1; } curr = curr.next; prev = prev.next; } curr = head; // change all the nodes with -1 to 0 while (curr != null) { if (curr.data == -1) curr.data = 0; curr = curr.next; } } // Driver code public static void main(String[] args) { // creating the linked list Node head = newNode(2); head.next = newNode(3); head.next.next = newNode(4); head.next.next.next = newNode(5); head.next.next.next.next = newNode(0); head.next.next.next.next.next = newNode(9); head.next.next.next.next.next.next = newNode(0); head.next.next.next.next.next.next.next = newNode(9); updateAdjacent(head); printList(head); } } /* This code contributed by PrinciRaj1992 */ |
Python3
# Python3 program to update the adjacent # nodes in a linked list # A linked list node class Node: def __init__(self, data): self.data = data self.next = None # Function to display the linked list def printList(node): # iterate until end of the # list is reached while node != None: print(node.data, end = " ") node = node.next # Function to udate the adjacent # nodes to zero def updateAdjacent(head): # Pointer to point to # the previous node prev = head # Pointer to the # current node curr = head.next # If the first node is zero and the # second node is not zero then change # the value to -1 if prev.data == 0 and curr.data != 0: curr.data = -1 # Temp variable to denote if the # current node is zero then the next # node will be changed to zero temp = 0 while curr != None: # if the temp vaiable is 1 and # current data is not zero if temp == 1 and curr.data != 0: # change the data to -1 curr.data = -1 temp = 0 # if the data is zero if curr.data == 0: # set the temp variable to 1 temp = 1 # set the previous node data to -1 prev.data = -1 curr = curr.next prev = prev.next curr = head # change all the nodes with -1 to 0 while curr != None: if curr.data == -1: curr.data = 0 curr = curr.next # Driver Code if __name__ == "__main__": # creating the linked list head = Node(2) head.next = Node(3) head.next.next = Node(4) head.next.next.next = Node(5) head.next.next.next.next = Node(0) head.next.next.next.next.next = Node(9) head.next.next.next.next.next.next = Node(0) head.next.next.next.next.next.next.next = Node(9) updateAdjacent(head) printList(head) # This code is contributed by Rituraj Jain |
C#
// C# program to update the adjacent // nodes in a linked list using System; class GFG { // A linked list node public class Node { public int data; public Node next; } // Utility function to create a new Node static Node newNode(int data) { Node temp = new Node(); temp.data = data; temp.next = null; return temp; } // Function to display the linked list static void printList(Node node) { // iterate until end of the list is reached while (node != null) { Console.Write(node.data + " "); node = node.next; } } // Function to udate the adjacent // nodes to zero static void updateAdjacent(Node head) { // Pointer to point to // the previous node Node prev = head; // Pointer to the // current node Node curr = head.next; // If the first node is zero and the // second node is not zero then change // the value to -1 if (prev.data == 0 && curr.data != 0) curr.data = -1; // Temp variable to denote if the // current node is zero then the next // node will be changed to zero int temp = 0; while (curr != null) { // if the temp vaiable is 1 and // current data is not zero if (temp == 1 && curr.data != 0) { // change the data to -1 curr.data = -1; temp = 0; } // if the data is zero if (curr.data == 0) { // set the temp variable to 1 temp = 1; // set the previous node data to -1 prev.data = -1; } curr = curr.next; prev = prev.next; } curr = head; // change all the nodes with -1 to 0 while (curr != null) { if (curr.data == -1) curr.data = 0; curr = curr.next; } } // Driver code public static void Main(String[] args) { // creating the linked list Node head = newNode(2); head.next = newNode(3); head.next.next = newNode(4); head.next.next.next = newNode(5); head.next.next.next.next = newNode(0); head.next.next.next.next.next = newNode(9); head.next.next.next.next.next.next = newNode(0); head.next.next.next.next.next.next.next = newNode(9); updateAdjacent(head); printList(head); } } // This code contributed by Rajput-Ji |
2 3 4 0 0 0 0 0
Recommended Posts:
- Sum of the nodes of a Singly Linked List
- Alternate Odd and Even Nodes in a Singly Linked List
- Product of the nodes of a Singly Linked List
- C Program to reverse each node value in Singly Linked List
- Select a Random Node from a Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Find the common nodes in two singly linked list
- Sum and Product of all Prime Nodes of a Singly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Reverse alternate K nodes in a Singly Linked List
- Delete all Non-Prime Nodes from a Singly Linked List
- Given only a pointer to a node to be deleted in a singly linked list, how do you delete it?
- Delete all Prime Nodes from a Circular Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



