📌  相关文章
📜  检查链接列表中连续节点的绝对差是否为1

📅  最后修改于: 2021-05-31 22:55:28             🧑  作者: Mango

给定一个单链表。任务是检查链接列表中连续节点之间的绝对差是否为1。

例子:

简单方法

  1. 用一个临时指针遍历该列表。
  2. 将标志变量初始化为true,指示连续节点的绝对差为1。
  3. 开始遍历列表。
  4. 检查连续节点的绝对差是否为1。
  5. 如果是,则继续遍历,否则将标志变量更新为零并停止进一步遍历。
    返回标志。

下面是上述方法的实现:

C++
// C++ program to check if absolute difference
// of consecutive nodes is 1 in Linked List
  
#include 
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;
}


Java
// 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


Python3
# Python3 program to check if absolute difference
# of consecutive nodes is 1 in Linked List
import math
  
# A linked list node
class Node: 
    def __init__(self, data): 
        self.data = data 
        self.next = None
  
# Utility function to create a new Node
def newNode(data):
    temp = Node(data)
    temp.data = data
    temp.next = None
    return temp
  
# Function to check if absolute difference
# of consecutive nodes is 1 in Linked List
def isConsecutiveNodes(head):
      
    # Create a temporary pointer
    # to traverse the list
    temp = head
  
    # Initialize a flag variable
    f = 1
  
    # Traverse through all the nodes
    # in the list
    while (temp):
  
        if (temp.next == None):
            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
if __name__=='__main__': 
  
    # creating the linked list
    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")
  
# This code is contributed by Srathore


C#
// 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


输出:
YES
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”