Find modular node in a linked list
Given a singly linked list and a number k, find the last node whose n%k == 0, where n is the number of nodes in the list.
Examples:
Input : list = 1->2->3->4->5->6->7
k = 3
Output : 6
Input : list = 3->7->1->9->8
k = 2
Output : 9
1. Take a pointer modularNode and initialize it with NULL. Traverse the linked list.
2. For every i%k=0, update modularNode.
C++
// C++ program to find modular node in a linked list #include <bits/stdc++.h> /* Linked list node */struct Node { int data; Node* next; }; /* Function to create a new node with given data */Node* newNode(int data) { Node* new_node = new Node; new_node->data = data; new_node->next = NULL; return new_node; } /* Function to find modular node in the linked list */Node* modularNode(Node* head, int k) { // Corner cases if (k <= 0 || head == NULL) return NULL; // Traverse the given list int i = 1; Node* modularNode = NULL; for (Node* temp = head; temp != NULL; temp = temp->next) { if (i % k == 0) modularNode = temp; i++; } return modularNode; } /* Driver program to test above function */int main(void) { Node* head = newNode(1); head->next = newNode(2); head->next->next = newNode(3); head->next->next->next = newNode(4); head->next->next->next->next = newNode(5); int k = 2; Node* answer = modularNode(head, k); printf("\nModular node is "); if (answer != NULL) printf("%d\n", answer->data); else printf("null\n"); return 0; } |
chevron_right
filter_none
Java
// A Java program to find modular node in a linked list public class GFG { // A Linkedlist node static class Node{ int data; Node next; Node(int data){ this.data = data; } } // Function to find modular node in the linked list static Node modularNode(Node head, int k) { // Corner cases if (k <= 0 || head == null) return null; // Traverse the given list int i = 1; Node modularNode = null; for (Node temp = head; temp != null; temp = temp.next) { if (i % k == 0) modularNode = temp; i++; } return modularNode; } // Driver code to test above function public static void main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5); int k = 2; Node answer = modularNode(head, k); System.out.print("Modular node is "); if (answer != null) System.out.println(answer.data); else System.out.println("null"); } } // This code is contributed by Sumit Ghosh |
chevron_right
filter_none
C#
// C# program to find modular node in a linked list using System; class GFG { // A Linkedlist node public class Node { public int data; public Node next; public Node(int data) { this.data = data; } } // Function to find modular node in the linked list static Node modularNode(Node head, int k) { // Corner cases if (k <= 0 || head == null) return null; // Traverse the given list int i = 1; Node modularNode = null; for (Node temp = head; temp != null; temp = temp.next) { if (i % k == 0) modularNode = temp; i++; } return modularNode; } // Driver code public static void Main(String[] args) { Node head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5); int k = 2; Node answer = modularNode(head, k); Console.Write("Modular node is "); if (answer != null) Console.WriteLine(answer.data); else Console.WriteLine("null"); } } // This code is contibuted by Rajput-JI |
chevron_right
filter_none
Output:
Modular node is 4
This article is contributed by Prakriti Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Find the balanced node in a Linked List
- Find the fractional (or n/k - th) node in linked list
- Find first node of loop in a linked list
- Find the second last node of a linked list in single traversal
- Find kth node from Middle towards Head of a Linked List
- Find the largest node in Doubly linked list
- Recursive Approach to find nth node from the end in the linked list
- Create new linked list from two given linked list with greater element at each node
- Swap Kth node from beginning with Kth node from end in a Linked List
- Remove every k-th node of the linked list
- Remove Nth node from end of the Linked List
- Delete Nth node from the end of the given linked list
- Squareroot(n)-th node in a Linked List
- Linked List | Set 2 (Inserting a node)
- Remove last node of the linked list
Improved By : Rajput-Ji



