📜  从循环单向链表中删除所有偶数和节点

📅  最后修改于: 2021-09-04 11:25:06             🧑  作者: Mango

给定一个包含N 个节点的循环单向链表,任务是从包含数字和为偶数的元素的列表中删除所有节点。

例子:

方法:思想是逐个遍历循环单向链表的节点,对于每个节点,通过迭代每个数字,找到节点中存在的值的数字和。如果数字和是偶数,则删除节点。否则,继续。

下面是上述方法的实现:

C++
// C++ program to remove all
// the Even Digit Sum Nodes from a
// circular singly linked list
 
#include 
using namespace std;
 
// Structure for a node
struct Node {
    int data;
    struct Node* next;
};
 
// Function to insert a node at the beginning
// of a Circular linked list
void push(struct Node** head_ref, int data)
{
    // Create a new node and make head as next
    // of it.
    struct Node* ptr1
        = (struct Node*)malloc(
            sizeof(struct Node));
 
    struct Node* temp = *head_ref;
    ptr1->data = data;
    ptr1->next = *head_ref;
 
    // If linked list is not NULL then
    // set the next of last node
    if (*head_ref != NULL) {
 
        // Find the node before head
        // and update next of it.
        while (temp->next != *head_ref)
            temp = temp->next;
 
        temp->next = ptr1;
    }
    else
 
        // Point for the first node
        ptr1->next = ptr1;
 
    *head_ref = ptr1;
}
 
// Function to delete the node from a
// Circular Linked list
void deleteNode(Node* head_ref, Node* del)
{
    struct Node* temp = head_ref;
 
    // If node to be deleted is head node
    if (head_ref == del)
        head_ref = del->next;
 
    // Traverse list till not found
    // delete node
    while (temp->next != del) {
        temp = temp->next;
    }
 
    // Copy the address of the node
    temp->next = del->next;
 
    // Finally, free the memory
    // occupied by del
    free(del);
 
    return;
}
 
// Function to find the digit sum
// for a number
int digitSum(int num)
{
    int sum = 0;
    while (num) {
        sum += (num % 10);
        num /= 10;
    }
 
    return sum;
}
 
// Function to delete all the Even Digit Sum Nodes
// from the singly circular linked list
void deleteEvenDigitSumNodes(Node* head)
{
    struct Node* ptr = head;
 
    struct Node* next;
 
    // Traverse the list till the end
    do {
 
        // If the node's data is Fibonacci,
        // delete node 'ptr'
        if (!(digitSum(ptr->data) & 1))
            deleteNode(head, ptr);
 
        // Point to the next node
        next = ptr->next;
        ptr = next;
 
    } while (ptr != head);
}
 
// Function to print nodes in a
// given Circular linked list
void printList(struct Node* head)
{
    struct Node* temp = head;
    if (head != NULL) {
        do {
            printf("%d ", temp->data);
            temp = temp->next;
        } while (temp != head);
    }
}
 
// Driver code
int main()
{
    // Initialize lists as empty
    struct Node* head = NULL;
 
    // Created linked list will be
    // 9->11->34->6->13->21
    push(&head, 21);
    push(&head, 13);
    push(&head, 6);
    push(&head, 34);
    push(&head, 11);
    push(&head, 9);
 
    deleteEvenDigitSumNodes(head);
 
    printList(head);
 
    return 0;
}


Java
// Java program to remove all
// the Even Digit Sum Nodes from a
// circular singly linked list
import java.util.*;
  
class GFG{
  
// Structure for a node
static class Node
{
    int data;
    Node next;
};
  
// Function to insert a node at the beginning
// of a Circular linked list
static Node push(Node head_ref, int data)
{
     
    // Create a new node and make head
    // as next of it.
    Node ptr1 = new Node();
  
    Node temp = head_ref;
    ptr1.data = data;
    ptr1.next = head_ref;
  
    // If linked list is not null then
    // set the next of last node
    if (head_ref != null)
    {
  
        // Find the node before head
        // and update next of it.
        while (temp.next != head_ref)
            temp = temp.next;
  
        temp.next = ptr1;
    }
    else
  
        // Point for the first node
        ptr1.next = ptr1;
  
    head_ref = ptr1;
    return head_ref;
}
  
// Function to delete the node from a
// Circular Linked list
static void deleteNode(Node head_ref, Node del)
{
    Node temp = head_ref;
  
    // If node to be deleted is head node
    if (head_ref == del)
        head_ref = del.next;
  
    // Traverse list till not found
    // delete node
    while (temp.next != del)
    {
        temp = temp.next;
    }
  
    // Copy the address of the node
    temp.next = del.next;
  
    // Finally, free the memory
    // occupied by del
    del = null;
  
    return;
}
  
// Function to find the digit sum
// for a number
static int digitSum(int num)
{
    int sum = 0;
     
    while (num > 0)
    {
        sum += (num % 10);
        num /= 10;
    }
    return sum;
}
  
// Function to delete all the Even Digit
// Sum Nodes from the singly circular
// linked list
static void deleteEvenDigitSumNodes(Node head)
{
    Node ptr = head;
    Node next;
  
    // Traverse the list till the end
    do
    {
         
        // If the node's data is Fibonacci,
        // delete node 'ptr'
        if (!(digitSum(ptr.data) % 2 == 1))
            deleteNode(head, ptr);
  
        // Point to the next node
        next = ptr.next;
        ptr = next;
    } while (ptr != head);
}
  
// Function to print nodes in a
// given Circular linked list
static void printList(Node head)
{
    Node temp = head;
    if (head != null)
    {
        do
        {
            System.out.printf("%d ", temp.data);
            temp = temp.next;
        } while (temp != head);
    }
}
  
// Driver code
public static void main(String[] args)
{
     
    // Initialize lists as empty
    Node head = null;
  
    // Created linked list will be
    // 9.11.34.6.13.21
    head = push(head, 21);
    head = push(head, 13);
    head = push(head, 6);
    head = push(head, 34);
    head = push(head, 11);
    head = push(head, 9);
  
    deleteEvenDigitSumNodes(head);
  
    printList(head);
}
}
  
// This code is contributed by Amit Katiyar


Python3
# Python3 program to remove all
# the Even Digit Sum Nodes from a
# circular singly linked list
  
# Structure for a node
class Node:
     
    def __init__(self, data):
         
        self.data = data
        self.next = None
     
# Function to insert a node at the beginning
# of a Circular linked list
def push(head_ref, data):
     
    # Create a new node and make head as next
    # of it.
    ptr1 = Node(data)
  
    temp = head_ref
    ptr1.data = data
    ptr1.next = head_ref
  
    # If linked list is not None then
    # set the next of last node
    if (head_ref != None):
  
        # Find the node before head
        # and update next of it.
        while (temp.next != head_ref):
            temp = temp.next
  
        temp.next = ptr1
     
    else:
  
        # Point for the first node
        ptr1.next = ptr1
  
    head_ref = ptr1
     
    return head_ref
 
# Function to deltete the node from a
# Circular Linked list
def delteteNode(head_ref, delt):
 
    temp = head_ref
  
    # If node to be delteted is head node
    if (head_ref == delt):
        head_ref = delt.next
  
    # Traverse list till not found
    # deltete node
    while (temp.next != delt):
        temp = temp.next
  
    # Copy the address of the node
    temp.next = delt.next
  
    # Finally, free the memory
    # occupied by delt
    del (delt)
  
    return
 
# Function to find the digit sum
# for a number
def digitSum(num):
 
    sum = 0
     
    while (num != 0):
        sum += (num % 10)
        num //= 10
     
    return sum
 
# Function to deltete all the Even Digit
# Sum Nodes from the singly circular linked list
def delteteEvenDigitSumNodes(head):
 
    ptr = head
    next = None
  
    # Traverse the list till the end
    while True:
         
        # If the node's data is Fibonacci,
        # deltete node 'ptr'
        if (not (digitSum(ptr.data) & 1)):
            delteteNode(head, ptr)
  
        # Point to the next node
        next = ptr.next
        ptr = next
         
        if (ptr == head):
            break
     
# Function to print nodes in a
# given Circular linked list
def printList(head):
 
    temp = head
     
    if (head != None):
        while True:
            print(temp.data, end = ' ')
            temp = temp.next
             
            if (temp == head):
                break
         
# Driver code
if __name__=='__main__':
     
    # Initialize lists as empty
    head = None
  
    # Created linked list will be
    # 9.11.34.6.13.21
    head = push(head, 21)
    head = push(head, 13)
    head = push(head, 6)
    head = push(head, 34)
    head = push(head, 11)
    head = push(head, 9)
  
    delteteEvenDigitSumNodes(head)
  
    printList(head)
  
# This code is contributed by rutvik_56


C#
// C# program to remove all
// the Even Digit Sum Nodes from a
// circular singly linked list
using System;
class GFG{
  
// Structure for a node
class Node
{
  public int data;
  public Node next;
};
  
// Function to insert a node
// at the beginning of a
// Circular linked list
static Node push(Node head_ref,
                 int data)
{
  // Create a new node and make head
  // as next of it.
  Node ptr1 = new Node();
 
  Node temp = head_ref;
  ptr1.data = data;
  ptr1.next = head_ref;
 
  // If linked list is not null then
  // set the next of last node
  if (head_ref != null)
  {
    // Find the node before head
    // and update next of it.
    while (temp.next != head_ref)
      temp = temp.next;
 
    temp.next = ptr1;
  }
  else
 
    // Point for the first node
    ptr1.next = ptr1;
 
  head_ref = ptr1;
  return head_ref;
}
  
// Function to delete the node from a
// Circular Linked list
static void deleteNode(Node head_ref,
                       Node del)
{
  Node temp = head_ref;
 
  // If node to be deleted is head node
  if (head_ref == del)
    head_ref = del.next;
 
  // Traverse list till not found
  // delete node
  while (temp.next != del)
  {
    temp = temp.next;
  }
 
  // Copy the address of the node
  temp.next = del.next;
 
  // Finally, free the memory
  // occupied by del
  del = null;
 
  return;
}
  
// Function to find the digit sum
// for a number
static int digitSum(int num)
{
  int sum = 0;
 
  while (num > 0)
  {
    sum += (num % 10);
    num /= 10;
  }
  return sum;
}
  
// Function to delete all the Even Digit
// Sum Nodes from the singly circular
// linked list
static void deleteEvenDigitSumNodes(Node head)
{
  Node ptr = head;
  Node next;
 
  // Traverse the list till the end
  do
  {
    // If the node's data is Fibonacci,
    // delete node 'ptr'
    if (!(digitSum(ptr.data) % 2 == 1))
      deleteNode(head, ptr);
 
    // Point to the next node
    next = ptr.next;
    ptr = next;
  } while (ptr != head);
}
  
// Function to print nodes in a
// given Circular linked list
static void printList(Node head)
{
  Node temp = head;
  if (head != null)
  {
    do
    {
      Console.Write("{0} ",
                    temp.data);
      temp = temp.next;
    } while (temp != head);
  }
}
  
// Driver code
public static void Main(String[] args)
{
  // Initialize lists as empty
  Node head = null;
 
  // Created linked list will be
  // 9.11.34.6.13.21
  head = push(head, 21);
  head = push(head, 13);
  head = push(head, 6);
  head = push(head, 34);
  head = push(head, 11);
  head = push(head, 9);
 
  deleteEvenDigitSumNodes(head);
  printList(head);
}
}
  
// This code is contributed by 29AjayKumar


Javascript


输出:
9 34 21

时间复杂度: O(KN) ,其中 N 是链表的大小,K 是链表的最大位数。

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