Using C programming Language,
2. Given a singly linked list (with a head pointer only) and an integer k, find the k^th node from the end of the linked list. Use a single path if possible.
Use the most efficient method, and state its run-time and space utilization in Big-O.
//please read all comments
#include <bits/stdc++.h>
using namespace std;
/* Node structure for linked list */
struct Node {
int data;
struct Node* next;
};
/* Function to get the kth node from the last*/
void printKthFromLast(struct Node* head, int k)
{
int len = 0, i;
struct Node* temp = head; //for iteration
// count the number of nodes
while (temp != NULL) {
temp = temp->next;
len++;
}
// check if k is more than list size
// more than length of the linked list
if (len < k)
return;
temp = head;
// Fetch the (len-k+1)th node from the start
for (i = 1; i < len - n + 1; i++)
temp = temp->next;
printf("%d",temp->data);
return;
}
//Function to add new node
void add(struct Node** head, int data)
{
struct Node* node = new Node();
// Put data
node->data = data;
node->next = (*head);
(*head) = node;
}
// Driver Code
int main()
{
// At very first time, list is empty
struct Node* head = NULL;
// create linked 1->12->90->29
add(&head, 29);
add(&head, 90);
add(&head, 12);
add(&head, 1);
printKthFromLast(head, 4);
return 0;
}
/* Output would be*/
1
/* Time complexity is O(n)
Linear
*/
Thanks ?
Using C programming Language, 2. Given a singly linked list (with a head pointer only) and...
You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...
in the c programming language. List *init() { head = ( List * ) malloc( sizeof( List ) ); if ( head == NULL ) { prtError( "Insufficient memory!" ); return( NULL ); } head->data = -1; head->next = NULL; tail = head; return ( head ); } /* Insert a new data element d into the list. */ /* Insert at the front of the list, right behind the dummy node. */ /* Return NULL if a new node...
Answer all questions
1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...
C programming
Write an iterative and recursive version of a function to print out a linked list from head to tail and then do the same for printing a linked list from tail to head. Assume a singly linked list in all cases and access only to a head pointer at the time of the function call. struct node; typedef struct node Node; struct node int data; Node next;
Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using only a constant amount of additional storage and no recursion. Assume the list object has a head pointer _head and consists of Node objects; a Node object has a pointer Node* _next
14) Create a singly linked list using only two variables: head and current. Traverse through the list void addToEnd (struct node *&head, int data){ //create node //if head==NULL //else } int main (){ node * head=NULL; addToEnd(head,1); addToEnd(head,2); addToEnd(head,3); //Traverse using current }
C++ Compsci 165: For your twelfth programming assignment you will be implementing a program that uses a linked list.You will be implementing the following structure and functions: struct LinkedList { int value; LinkedList *next; }; Note that with a singly linked list, you need to maintain a head pointer (pointer to the beginning of the list). Typically a tail pointer (pointer to the end of the list) is not maintained in a singly linked list (because you can only iterate...
1)Given a singly linked list contains four nodes and simply show as 4->3->2->1, where the head reference refers to the first node contains an Integer with value 4. If Node curr = head; curr= curr.next; are applied to this list, what is the number you can find in the first node? 2) Given a singly linked list contains 6 nodes, which is simply shown as 1->2->3->4->5->6. Assume head refers to the first node (contains 1) on the list. How many...
c++
5. You're given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete as few nodes as possible so that the list does not contain any value more than once. The given head pointer may be null indicating that the list is empty. Enter numbers ending with -999 for the linked list: 3 7 11 11 11 24 27 30 42 42 47 55 55 78 89-999...
Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...