Check if absolute difference of consecutive nodes is 1 in Linked List
Last Updated :
15 Sep, 2022
Given a Singly Linked List. The task is to check if the absolute difference between the consecutive nodes in the linked list is 1 or not.
Examples:
Input : List = 2->3->4->5->4->3->2->1->NULL
Output : YES
Explanation : The difference between adjacent nodes in the list is 1. Hence the given list is a Jumper Sequence.
Input : List = 2->3->4->5->3->4->NULL
Output : NO
Simple Approach:
- Take a temporary pointer to traverse the list.
- Initialize a flag variable to true which indicates that the absolute difference of consecutive nodes is 1.
- Start traversing the list.
- Check if the absolute difference of consecutive nodes is 1 or not.
- If Yes then continue traversal otherwise update flag variable to zero and stop traversing any further.
Return flag.
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;
}
bool isConsecutiveNodes(Node* head)
{
Node* temp = head;
int f = 1;
while (temp) {
if (!temp->next)
break ;
if ( abs ((temp->data) - (temp->next->data)) != 1) {
f = 0;
break ;
}
temp = temp->next;
}
return f;
}
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(4);
head->next->next->next->next->next = newNode(3);
head->next->next->next->next->next->next = newNode(2);
head->next->next->next->next->next->next->next = newNode(1);
if (isConsecutiveNodes(head))
cout << "YES" ;
else
cout << "NO" ;
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 int isConsecutiveNodes(Node head)
{
Node temp = head;
int f = 1 ;
while (temp != null )
{
if (temp.next == null )
break ;
if (Math.abs((temp.data) - (temp.next.data)) != 1 )
{
f = 0 ;
break ;
}
temp = temp.next;
}
return f;
}
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( 4 );
head.next.next.next.next.next = newNode( 3 );
head.next.next.next.next.next.next = newNode( 2 );
head.next.next.next.next.next.next.next = newNode( 1 );
if (isConsecutiveNodes(head) == 1 )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def newNode(data):
temp = Node(data)
temp.data = data
temp. next = None
return temp
def isConsecutiveNodes(head):
temp = head
f = 1
while (temp):
if (temp. next = = None ):
break
if ( abs ((temp.data) -
(temp. next .data)) ! = 1 ) :
f = 0
break
temp = temp. next
return f
if __name__ = = '__main__' :
head = newNode( 2 )
head. next = newNode( 3 )
head. next . next = newNode( 4 )
head. next . next . next = newNode( 5 )
head. next . next . next . next = newNode( 4 )
head. next . next . next . next . next = newNode( 3 )
head. next . next . next . next . next . next = newNode( 2 )
head. next . next . next . next . next . next . next = newNode( 1 )
if (isConsecutiveNodes(head)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public 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 int isConsecutiveNodes(Node head)
{
Node temp = head;
int f = 1;
while (temp != null )
{
if (temp.next == null )
break ;
if (Math.Abs((temp.data) - (temp.next.data)) != 1)
{
f = 0;
break ;
}
temp = temp.next;
}
return f;
}
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(4);
head.next.next.next.next.next = newNode(3);
head.next.next.next.next.next.next = newNode(2);
head.next.next.next.next.next.next.next = newNode(1);
if (isConsecutiveNodes(head) == 1)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function newNode( data)
{
var temp = new Node();
temp.data = data;
temp.next = null ;
return temp;
}
function isConsecutiveNodes( head)
{
var temp = head;
let f = 1;
while (temp != null )
{
if (temp.next == null )
break ;
if (Math.abs((temp.data) - (temp.next.data)) != 1)
{
f = 0;
break ;
}
temp = temp.next;
}
return f;
}
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(4);
head.next.next.next.next.next = newNode(3);
head.next.next.next.next.next.next = newNode(2);
head.next.next.next.next.next.next.next = newNode(1);
if (isConsecutiveNodes(head) == 1)
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Complexity Analysis:
- Time Complexity: O(n) //since only one traversal of the linked list is enough to perform all operations hence the time taken by the algorithm is linear
- Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant
Share your thoughts in the comments
Please Login to comment...