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.
Insert a node at a specific position in a linked list
Insert a node at a specific position in a linked list
+ 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 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 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 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; }
+ 1 comment its giving me illegal static declaration in the inner class error. what shound i do?
Load more conversations
Sort 1399 Discussions, By:
Please Login in order to post a comment