Sort by

recency

|

711 Discussions

|

  • + 0 comments

    Solution in C#, using recursion:

    public static Node insert(Node head,int data)
    {
        if (head == null)
        {
            head = new Node(data);
            return head;
        }
        var newHead = insert(head.next, data);
    
        head.next= newHead;
    
        return head;
    }
    
  • + 0 comments

    In C++

    #include <bits/stdc++.h>
    using namespace std;
    
    int main(){
        int n;
        cin >> n;
        
        vector<int> f(n);
        
        for(int i = 0; i < f.size(); i++){
            cin >> f[i];
        }
        
        for(int i = 0; i < f.size(); i++){
            cout << f[i] << " ";
        }
    }
    

    `

  • + 0 comments

    python 3:

    def insert(self, head, data):
        new_node = Node(data)
        if head is None:
            head = new_node
        else:
            current = head
            while current.next:
                current = current.next
            current.next = new_node
        return head
    
  • + 0 comments

    javascript solution:

    this.insert=function(head, data){
              //complete this method
              
              if(head == null){
                  return {data: data, next: null}
              }
                  let curr = head
                  while(curr !== null){
                      if(curr.next == null){
                        let newnode = {data: data, next: null}
                        curr.next = newnode
                        break
                      }
                      curr = curr.next
                  }
              return head   
        };
    
  • + 0 comments

    Java Solution:

    class Solution {
    
        public static  Node insert(Node head,int data) {
            //Complete this method
            Node newNode = new Node(data);
            newNode.next = null;
            
            if(head == null){
                return head = newNode;
            }
            
            Node curr = head;
            while(curr.next != null){
                curr = curr.next;
            }
            curr.next = newNode;
            return head;
        }