6 kyu

Fun with lists: map

949 of 1,697janitormeir

Description:

Implement the method map, which accepts a linked list (head) and a mapping function, and returns a new linked list (head) where every element is the result of applying the given mapping method to each element of the original list.

For example: Given the list: 1 -> 2 -> 3, and the mapping function x => x * 2, map should return 2 -> 4 -> 6

The linked list is defined as follows:

function Node(data, next = null) {
  this.data = data;
  this.next = next;
}
class Node<T> {
  public T data;
  public Node<T> next;
  
  Node(T data, Node next) {
    this.data = data;
    this.next = next;
  }
  
  Node(T data) {
    this(data, null);
  }
}
class Node {
  public $data, $next;
  public function __construct($data, $next = NULL) {
    $this->data = $data;
    $this->next = $next;
  }
}
class Node<T> 
{
    public T data;
    public Node<T> next;
    
    public Node<T>(T data){
        this.data = data;
    }
    
    public Node<T>(T data, Node<T> next){
        this.data = data;
        this.next = next;
    }
}
struct Node {
  struct Node *next;
  int data;
};

Note: the list may be null and can hold any type of value.

Good luck!

This kata is part of fun with lists series:

Lists
Functional Programming
Fundamentals

Stats:

CreatedNov 11, 2016
PublishedNov 11, 2016
Warriors Trained4474
Total Skips381
Total Code Submissions9272
Total Times Completed1697
JavaScript Completions949
Java Completions443
PHP Completions73
C# Completions208
C Completions73
Total Stars90
% of votes with a positive feedback rating92% of 342
Total "Very Satisfied" Votes297
Total "Somewhat Satisfied" Votes36
Total "Not Satisfied" Votes9
Total Rank Assessments8
Average Assessed Rank
6 kyu
Highest Assessed Rank
6 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • janitormeir Avatar
  • smile67 Avatar
  • donaldsebleung Avatar
  • kazk Avatar
  • BratWiekszy Avatar
  • Voile Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • akar-0 Avatar
Ad