Check if absolute difference of consecutive nodes is 1 in Linked List

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:

  1. Take a temporary pointer to traverse the list.
  2. Initialize a flag variable to true which indicates that the absolute difference of consecutive nodes is 1.
  3. Start traversing the list.
  4. Check if the absolute difference of consecutive nodes is 1 or not.
  5. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if absolute difference
// of consecutive nodes is 1 in 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 check if absolute difference
// of consecutive nodes is 1 in Linked List
bool isConsecutiveNodes(Node* head)
{
    // Create a temporary pointer
    // to traverse the list
    Node* temp = head;
  
    // Initialize a flag variable
    int f = 1;
  
    // Traverse through all the nodes
    // in the list
    while (temp) {
  
        if (!temp->next)
            break;
  
        // count the number of jumper sequence
        if (abs((temp->data) - (temp->next->data)) != 1) {
            f = 0;
            break;
        }
  
        temp = temp->next;
    }
  
    // return flag
    return f;
}
  
// 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(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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check if absolute difference
// of consecutive nodes is 1 in 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 check if absolute difference
// of consecutive nodes is 1 in Linked List
static int isConsecutiveNodes(Node head)
{
    // Create a temporary pointer
    // to traverse the list
    Node temp = head;
  
    // Initialize a flag variable
    int f = 1;
  
    // Traverse through all the nodes
    // in the list
    while (temp != null
    {
  
        if (temp.next == null)
            break;
  
        // count the number of jumper sequence
        if (Math.abs((temp.data) - (temp.next.data)) != 1)
        {
            f = 0;
            break;
        }
  
        temp = temp.next;
    }
  
    // return flag
    return f;
}
  
// 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(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");
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check if absolute difference
// of consecutive nodes is 1 in Linked List
using System;
      
public 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 check if absolute difference
// of consecutive nodes is 1 in Linked List
static int isConsecutiveNodes(Node head)
{
    // Create a temporary pointer
    // to traverse the list
    Node temp = head;
   
    // Initialize a flag variable
    int f = 1;
   
    // Traverse through all the nodes
    // in the list
    while (temp != null
    {
   
        if (temp.next == null)
            break;
   
        // count the number of jumper sequence
        if (Math.Abs((temp.data) - (temp.next.data)) != 1)
        {
            f = 0;
            break;
        }
   
        temp = temp.next;
    }
   
    // return flag
    return f;
}
   
// 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(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");
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


Output:

YES


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : 29AjayKumar, Rajput-Ji