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
- 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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " " ;
node = node->next;
}
}
void updateAdjacent(Node* head)
{
Node* prev = head;
Node* curr = head->next;
if (prev->data == 0 && curr->data != 0)
curr->data = -1;
int temp = 0;
while (curr != NULL) {
if (temp == 1 && curr->data != 0) {
curr->data = -1;
temp = 0;
}
if (curr->data == 0) {
temp = 1;
prev->data = -1;
}
curr = curr->next;
prev = prev->next;
}
curr = head;
while (curr != NULL) {
if (curr->data == -1)
curr->data = 0;
curr = curr->next;
}
}
int main()
{
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
class GFG
{
static class Node
{
int data;
Node next;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.next = null ;
return temp;
}
static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data + " " );
node = node.next;
}
}
static void updateAdjacent(Node head)
{
Node prev = head;
Node curr = head.next;
if (prev.data == 0 && curr.data != 0 )
curr.data = - 1 ;
int temp = 0 ;
while (curr != null )
{
if (temp == 1 && curr.data != 0 )
{
curr.data = - 1 ;
temp = 0 ;
}
if (curr.data == 0 )
{
temp = 1 ;
prev.data = - 1 ;
}
curr = curr.next;
prev = prev.next;
}
curr = head;
while (curr != null )
{
if (curr.data == - 1 )
curr.data = 0 ;
curr = curr.next;
}
}
public static void main(String[] args)
{
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);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def printList(node):
while node ! = None :
print (node.data, end = " " )
node = node. next
def updateAdjacent(head):
prev = head
curr = head. next
if prev.data = = 0 and curr.data ! = 0 :
curr.data = - 1
temp = 0
while curr ! = None :
if temp = = 1 and curr.data ! = 0 :
curr.data = - 1
temp = 0
if curr.data = = 0 :
temp = 1
prev.data = - 1
curr = curr. next
prev = prev. next
curr = head
while curr ! = None :
if curr.data = = - 1 :
curr.data = 0
curr = curr. next
if __name__ = = "__main__" :
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)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.next = null ;
return temp;
}
static void printList(Node node)
{
while (node != null )
{
Console.Write(node.data + " " );
node = node.next;
}
}
static void updateAdjacent(Node head)
{
Node prev = head;
Node curr = head.next;
if (prev.data == 0 && curr.data != 0)
curr.data = -1;
int temp = 0;
while (curr != null )
{
if (temp == 1 && curr.data != 0)
{
curr.data = -1;
temp = 0;
}
if (curr.data == 0)
{
temp = 1;
prev.data = -1;
}
curr = curr.next;
prev = prev.next;
}
curr = head;
while (curr != null )
{
if (curr.data == -1)
curr.data = 0;
curr = curr.next;
}
}
public static void Main(String[] args)
{
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);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function newNode(data) {
var temp = new Node();
temp.data = data;
temp.next = null ;
return temp;
}
function printList(node) {
while (node != null ) {
document.write(node.data + " " );
node = node.next;
}
}
function updateAdjacent(head) {
var prev = head;
var curr = head.next;
if (prev.data == 0 && curr.data != 0)
curr.data = -1;
var temp = 0;
while (curr != null ) {
if (temp == 1 && curr.data != 0) {
curr.data = -1;
temp = 0;
}
if (curr.data == 0) {
temp = 1;
prev.data = -1;
}
curr = curr.next;
prev = prev.next;
}
curr = head;
while (curr != null ) {
if (curr.data == -1)
curr.data = 0;
curr = curr.next;
}
}
var 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);
</script>
|
Time Complexity: O(n) where n is no of nodes in given linked list
Last Updated :
12 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...