📜  从包含斐波那契数列的双向链表中删除所有节点

📅  最后修改于: 2021-09-07 04:43:50             🧑  作者: Mango

给定一个包含N 个节点的双向链表,任务是从包含斐波那契数列的列表中删除所有节点。
例子:

方法:这个想法是使用散列来存储和检查斐波那契数。

  1. 遍历整个双向链表,得到链表中的最大值。
  2. 现在,为了检查斐波那契数,构建一个哈希表,其中包含所有小于或等于链表中最大值的斐波那契数。
  3. 最后,将双向链表的节点一一遍历,删除包含斐波那契数作为其数据值的节点。

下面是上述方法的实现:

C++
// C++ implementation to delete all
// Fibonacci nodes from the
// doubly linked list
 
#include
 
using namespace std;
 
// Node of the doubly linked list
struct Node {
    int data;
    Node *prev, *next;
};
 
// Function to insert a node at the beginning
// of the Doubly Linked List
void push(Node** head_ref, int new_data)
{
    // Allocate the node
    Node* new_node
        = (Node*)malloc(sizeof(struct Node));
 
    // Insert the data
    new_node->data = new_data;
 
    // Since we are adding at the beginning,
    // prev is always NULL
    new_node->prev = NULL;
 
    // Link the old list off the new node
    new_node->next = (*head_ref);
 
    // Change the prev of head node to new node
    if ((*head_ref) != NULL)
        (*head_ref)->prev = new_node;
 
    // Move the head to point to the new node
    (*head_ref) = new_node;
}
 
// Function to find the largest
// nodes in the Doubly Linked List
int LargestInDLL(struct Node** head_ref)
{
    struct Node *max, *temp;
 
    // Initialize two-pointer temp
    // and max on the head node
    temp = max = *head_ref;
 
    // Traverse the whole doubly linked list
    while (temp != NULL) {
 
        // If temp's data is greater than
        // the max's data, then max = temp
        // and return max->data
        if (temp->data > max->data)
            max = temp;
 
        temp = temp->next;
    }
    return max->data;
}
 
// Function to create hash table to
// check Fibonacci numbers
void createHash(set& hash, int maxElement)
{
    int prev = 0, curr = 1;
    hash.insert(prev);
    hash.insert(curr);
 
    // Inserting the Fibonacci numbers
    // until the maximum element in the
    // Linked List
    while (curr  pointer to head node pointer.
// del --> pointer to node to be deleted
void deleteNode(Node** head_ref, Node* del)
{
    // Base case
    if (*head_ref == NULL || del == NULL)
        return;
 
    // If the node to be deleted is head node
    if (*head_ref == del)
        *head_ref = del->next;
 
    // Change next only if node to be
    // deleted is not the last node
    if (del->next != NULL)
        del->next->prev = del->prev;
 
    // Change prev only if node to be
    // deleted is not the first node
    if (del->prev != NULL)
        del->prev->next = del->next;
 
    // Finally, free the memory
    // occupied by del
    free(del);
 
    return;
}
 
// Function to delete all fibonacci nodes
// from the doubly linked list
void deleteFibonacciNodes(Node** head_ref)
{
    // Find the largest node value
    // in Doubly Linked List
    int maxEle = LargestInDLL(head_ref);
 
    // Creating a set containing
    // all the fibonacci numbers
    // upto the maximum data value
    // in the Doubly Linked List
    set hash;
    createHash(hash, maxEle);
 
    Node* ptr = *head_ref;
    Node* next;
 
    // Iterating through the linked list
    while (ptr != NULL) {
        next = ptr->next;
 
        // If node's data is fibonacci,
        // delete node 'ptr'
        if (hash.find(ptr->data) != hash.end())
            deleteNode(head_ref, ptr);
 
        ptr = next;
    }
}
 
// Function to print nodes in a
// given doubly linked list
void printList(Node* head)
{
    while (head != NULL) {
        cout 


Python3
# Python3 implementation to deltete all
# Fibonacci nodes from the
# doubly linked list
 
# Node of the doubly linked list
class Node:
     
    def __init__(self):
         
        self.data = 0
        self.next = None
        self.prev = None
     
# Function to add a node at the beginning
# of the Doubly Linked List
def push(head_ref, new_data):
 
    # Allocate the node
    new_node = Node()
 
    # Insert the data
    new_node.data = new_data;
 
    # Since we are adding at the beginning,
    # prev is always None
    new_node.prev = None;
 
    # Link the old list off the new node
    new_node.next = (head_ref);
 
    # Change the prev of head node to new node
    if ((head_ref) != None):
        (head_ref).prev = new_node;
 
    # Move the head to poto the new node
    (head_ref) = new_node;
     
    return head_ref
 
# Function to find the largest
# nodes in the Doubly Linked List
def LargestInDLL(head_ref):
 
    max = None
    temp = None
 
    # Initialize two-pointer temp
    # and max on the head node
    temp = max = head_ref;
 
    # Traverse the whole doubly linked list
    while (temp != None):
 
        # If temp's data is greater than
        # the max's data, then max = temp
        # and return max.data
        if (temp.data > max.data):
            max = temp;
 
        temp = temp.next;
     
    return max.data;
 
 
# Function to create hashset table to
# check Fibonacci numbers
def createHash( hashset, maxElement):
 
    prev = 0
    curr = 1;
    hashset.add(prev);
    hashset.add(curr);
 
    # Inserting the Fibonacci numbers
    # until the maximum element in the
    # Linked List
    while (curr <= maxElement):
        temp = curr + prev;
        hashset.add(temp);
        prev = curr;
        curr = temp;
     
# Function to deltete a node
# in a Doubly Linked List.
# head_ref -. pointer to head node pointer.
# delt -. pointer to node to be delteted
def delteteNode(head_ref, delt):
 
    # Base case
    if (head_ref == None or delt == None):
        return;
 
    # If the node to be delteted is head node
    if (head_ref == delt):
        head_ref = delt.next;
 
    # Change next only if node to be
    # delteted is not the last node
    if (delt.next != None):
        delt.next.prev = delt.prev;
 
    # Change prev only if node to be
    # delteted is not the first node
    if (delt.prev != None):
        delt.prev.next = delt.next;
 
    # Finally, free the memory
    # occupied by delt
    del(delt);
 
    return;
 
# Function to deltete all fibonacci nodes
# from the doubly linked list
def delteteFibonacciNodes(head_ref):
 
    # Find the largest node value
    # in Doubly Linked List
    maxEle = LargestInDLL(head_ref);
 
    # Creating a set containing
    # all the fibonacci numbers
    # upto the maximum data value
    # in the Doubly Linked List
    hashset = set()
    createHash(hashset, maxEle);
 
    ptr = head_ref;
    next=None
 
    # Iterating through the linked list
    while (ptr != None):
        next = ptr.next;
 
        # If node's data is fibonacci,
        # deltete node 'ptr'
        if (ptr.data in hashset):
            delteteNode(head_ref, ptr);
 
        ptr = next;
     
# Function to prnodes in a
# given doubly linked list
def printList(head):
 
    while (head != None):
         
        print(head.data, end = ' ')
        head = head.next;
     
# Driver program
if __name__=='__main__':
 
    head = None;
 
    # Create the doubly linked list
    # 15 <. 16 <. 8 <. 6 <. 13
    head = push(head, 13);
    head = push(head, 6);
    head = push(head, 8);
    head = push(head, 16);
    head = push(head, 15);
     
    print("Original List: ", end='')
    printList(head);
 
    delteteFibonacciNodes(head);
     
    print("\nModified List: ", end='')
    printList(head);
 
# This code is contributed by rutvik_56


输出:
Original List: 15 16 8 6 13 
Modified List: 15 16 6

时间复杂度: O(N) ,其中 N 是节点总数。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live