热门标签 | 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。

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


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • JavaSE笔试题-接口、抽象类、多态等问题解答
    本文解答了JavaSE笔试题中关于接口、抽象类、多态等问题。包括Math类的取整数方法、接口是否可继承、抽象类是否可实现接口、抽象类是否可继承具体类、抽象类中是否可以有静态main方法等问题。同时介绍了面向对象的特征,以及Java中实现多态的机制。 ... [详细]
  • 本文介绍了解决Netty拆包粘包问题的一种方法——使用特殊结束符。在通讯过程中,客户端和服务器协商定义一个特殊的分隔符号,只要没有发送分隔符号,就代表一条数据没有结束。文章还提供了服务端的示例代码。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
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社区 版权所有