热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

检查链表中连续节点的绝对差是否为1

检查链表中连续节点的绝对差是否为1原文:https://www

检查链表中连续节点的绝对差是否为 1

原文:https://www.geeksforgeeks.org/check-if-absolute-difference-of-consecutive-nodes-is-1-in-linked-list/

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

示例

输入list = 2 -> 3 -> 4 -> 5 -> 4 -> 3 -> 2 -> 1 -> NULL

输出Yes

说明:列表中相邻节点之间的差为 1。因此,给定的列表是一个跳线序列。

输入list = 2 -> 3 -> 4 -> 5 -> 3 -> 4 -> NULL

输出No

简单方法


  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





如果您喜欢 GeeksforGeeks 并希望做出贡献,则还可以使用 tribution.geeksforgeeks.org 撰写文章,或将您的文章邮寄至 tribution@geeksforgeeks.org。 查看您的文章出现在 GeeksforGeeks 主页上,并帮助其他 Geeks。

如果您发现任何不正确的地方,请单击下面的“改进文章”按钮,以改进本文。


推荐阅读
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 从零学Java(10)之方法详解,喷打野你真的没我6!
    本文介绍了从零学Java系列中的第10篇文章,详解了Java中的方法。同时讨论了打野过程中喷打野的影响,以及金色打野刀对经济的增加和线上队友经济的影响。指出喷打野会导致线上经济的消减和影响队伍的团结。 ... [详细]
author-avatar
陈炘宇_573
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有