We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Linked Lists
  4. Insert a node at a specific position in a linked list
  5. Discussions

Insert a node at a specific position in a linked list

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1399 Discussions, By:

recency

Please Login in order to post a comment

  • mrashidcit
    1 week ago+ 0 comments

    Here is my Solution in Java :

    public static SinglyLinkedListNode insertNodeAtPosition(SinglyLinkedListNode llist, int data, int position) {
            
            
            SinglyLinkedListNode currentNode = llist;
            int index = 0;
            while(currentNode != null) {
                
                if (index == position) {
                    SinglyLinkedListNode tempNode = new SinglyLinkedListNode(currentNode.data);
                    tempNode.next = currentNode.next;
                    currentNode.data = data;
                    currentNode.next = tempNode;
                    
                    
                }
                
                currentNode = currentNode.next;
                index++;
            }
            
            return llist;
    
        }
    
    0|
    Permalink
  • anthansson
    1 week ago+ 0 comments

    Go solution

    func insertNodeAtPosition(llist *SinglyLinkedListNode, data int32, position int32) *SinglyLinkedListNode {
    	// find pos
    	currNode := llist
    	for position > 1 {
    		position--
    		currNode = currNode.next
    	}
    
    	// insert node
    	newNode := &SinglyLinkedListNode{data: data}
    	newNode.next = currNode.next
    	currNode.next = newNode
    	return llist
    }
    
    0|
    Permalink
  • macdonald_l_kyn
    1 week ago+ 0 comments

    Can anyone tell me what's wrong with my Python code

    def insertNodeAtPosition(llist, data, position):
        i = 0
        while llist:
            if i != position:
                print(llist.data, end=" ")
                llist = llist.next
                i += 1
            else:
                print(data, end=" ")
                i += 1
    
    0|
    Permalink
  • filipemunhoz
    2 weeks ago+ 0 comments

    Java if (position == 0) { int lastData = llist.data; llist.data = data;

            if(llist.next == null) {
                llist.next = new SinglyLinkedListNode(lastData);
                return llist;
            }
            insertNodeAtPosition(llist.next, lastData, 0);
        } else {
    
                insertNodeAtPosition(llist.next, data, position - 1);
        }
        return llist;
    }
    
    0|
    Permalink
  • sridhanvi_moksh1
    2 weeks ago+ 1 comment

    its giving me illegal static declaration in the inner class error. what shound i do?

    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy