/*Given the head of a linked list, find and return the kth node of the linked list
*Assume head is the first node
*If k is larger than the size of the linked list, return NULL
*
c++
* Example: n1 -> n2 -> n3 -> n4 -> n5, k = 3
* Return &n3
*/
Node* findKthNode(Node *head, int k){
return NULL;
//STUB: edit with the correct output, according to the lab instructions, using recursion
} must use recursive solutions
Explanation:-
just like the iterative function, we can implement recursive function by,
we have passed the head of the linked-list and number of the node to be found.
So for our recursive function, we can have,
if k<=0 then it's invalid k. we will return null.
else if head==null the also we will return null.
and if k==1 we will return head.
otherwise, we will return findKthNode(head,k-1)
so basically we are increasing head to head->next and decrease
the k by 1.
so if the head is null and k is greater than one according to our condition we will return null.
Required Code:-
// Required recursive function to find kth node..
Node* findkthNode(Node* head, int k){
// If invalid k
if (k < 1)
return NULL;
// If linked list is empty
// Or if k is greater than linked-list size
if (head == NULL)
return NULL;
// Base case (start needs to be returned)
if (k == 1)
return head;
return findkthNode(head->next,k-1);
}
Screenshot of required code:-
Full code including driver function :-
#include<iostream>
using namespace std;
struct Node {
string data;
struct Node* next;
};
// Required recursive function to find kth node..
Node* findkthNode(Node* head, int k){
// If invalid k
if (k < 1)
return NULL;
// If linked list is empty
// Or if k is greater than linked-list size
if (head == NULL)
return NULL;
// Base case (start needs to be deleted)
if (k == 1)
return head;
return findkthNode(head->next,k-1);
}
/* Function to insert a node at the beginning */
void push(struct Node **head_ref,string new_data)
{
struct Node *new_node = new Node;
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
/*Function to print a linked list */
void printLL(struct Node *head)
{
while (head!=NULL)
{
cout << head->data << " ";
head = head->next;
}
printf("\n");
}
/* Driver program*/
int main(){
struct Node *head = NULL;
/*I have just added function to
enter the data at beginning
So below 5 line create Linkedlist
of n1->n2->n3->n4->n5->n6*/
push(&head,"n6");
push(&head,"n5");
push(&head,"n4");
push(&head,"n3");
push(&head,"n2");
push(&head,"n1");
/*print current linked-list*/
cout<<"Current Link-list\n";
printLL(head);
/*node to be returned*/
cout<<"Enter the node number to find :";
int k;
cin>>k;
struct Node *kthNode = NULL;
kthNode = findkthNode(head,k);
if(kthNode!=NULL)
cout<<kthNode->data<<'\n';
else
cout<<"K is either negative or greater than linked-list
size\n";
return 0;
}
Screenshot of Full Code:-
Output -1:-
Output 2:-
I hope you got the answer and got the logic behind it.
Please like and comment if you have any queries.
/*Given the head of a linked list, find and return the kth node of the linked...
Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the list is empty. * * Parameter other points to the first node in a linked list, or is * NULL if the list is empty. * * Extend the linked list pointed to by head so that it contains * copies of the values stored in the linked list pointed to by other. *...
Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...
1.Implement recursive and iterative delete functions for linked lists. Node declaration of the linked list is given below. struct node { int info; struct node *next; }; typedef struct node node; You can assume that all the nodes in the linked list are distinct and each node appears in the list at most once. Prototype of the functions are given below. node *delete(node *head, int k) node *recursivedelete(node *head, int k) • delete deletes the node with info k from...
C++ Consider the following structure of node and linked list. struct Node { int key; Node *next; }; 10 -> 20 -> 30 -> 10 -> 10 -> 50 -> 10 -> NULL What will be the output of following pseudo-code? Consider head is the pointer to the first node of above linked list. Node *walker = head; int count = 0; while(walker!= NULL && count < 3) { if(walker->key == 10) { count = count + 1; } walker...
Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...
Given the node structure and the head pointer (headptr) of a linked list write a code to move the last element to the head of the list. struct node { int value; struct node *next; };
JAVA - Write a recursive function that takes a linked list as input and returns all of the elements in the linked list. Input: 1, 2, 3, 4, 5 Output: (1+2+3+4+5)/5 = 3 What I have: public static int findMean (Node head, Node cur, int n){ int average = 0; if (head == null){ if(n == 0) return 0; } if (n > 0){ int sum = n + findMean(head, head.next, n -1); average = (sum)/n; } return average; }...
This is a recursive function to delete all even nodes from a linked list. node *deleteEven(node *head) if (head == NULL) return head; if (head->data % 2 == 0) { node *temp = head; free(temp); return deleteEven(head->next); } else { head->next = deleteEven(head->next); return head; } } What is the purpose of heaving to set "head->next = deleteEven(head->next);" instead of just having "head = head->next;" in one of the lines above and just calling "deleteEven(head->next);"?
Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...