Data Structures | Linked List | Question 11

Last Updated :
Discuss
Comments

Consider the function f defined below. 

C++
#include <iostream>  struct item {     int data;     struct item *next; };  int f(struct item *p) {     return (         (p == NULL) ||          (p->next == NULL) ||          ((p->data <= p->next->data) && f(p->next))     ); } 
c
struct item  {    int data;    struct item * next;  };   int f(struct item *p)  {    return (           (p == NULL) ||            (p->next == NULL) ||            (( p->data <= p->next->data) && f(p->next))          );  }  
Java
class Item {     int data;     Item next;      Item(int data) {         this.data = data;         this.next = null;     } }  public class Main {     public static boolean f(Item p) {         return (             (p == null) ||              (p.next == null) ||              ((p.data <= p.next.data) && f(p.next))         );     } } 
Python
class Item:     def __init__(self, data):         self.data = data         self.next = None   def f(p):     return (         p is None or          p.next is None or          (p.data <= p.next.data and f(p.next))     ) 
JavaScript
class Item {     constructor(data) {         this.data = data;         this.next = null;     } }  function f(p) {     return (         p === null ||          p.next === null ||          (p.data <= p.next.data && f(p.next))     ); } 

For a given linked list p, the function f returns 1 if and only if (GATE CS 2003)

not all elements in the list have the same data value.

the elements in the list are sorted in non-decreasing order of data value

the elements in the list are sorted in non-increasing order of data value

None of them

Share your thoughts in the comments