'Inserting a node at a specific position in a Linked list
I am given the pointer to the head node of a linked list, an integer to add to the list and the position at which the integer must be inserted. After inserting this node at the desired position I need to return the head node.
The code that I have written is not working for some reason and goes in an infinite loop.
class Node {
int data;
Node next;
}
Node InsertNth(Node head, int data, int position) {
int count = 0;
Node node = head;
Node prev = null;
while(count != position){
count++;
node = node.next;
prev = node;
}
Node newNode = new Node();
newNode.data = data;
newNode.next = node;
if(count == 0){
head = newNode;
}else{
prev.next = newNode;
}
return head;
}
Solution 1:[1]
Firstly, I find the position. Then insert the new Node at that position by changing pointers.
Node InsertNth(Node head, int data, int position) {
Node newNode= new Node();
newNode.data=data;
Node ptr = head;
pos = pos - 1;
for (int i = 1; ; i++) {
if(ptr==null)
{ break;
}
if (i == pos) {
newNode.next=ptr.next;
ptr.next=newNode;
break;
}
ptr = ptr.next;
}
}
Solution 2:[2]
public static Node InsertNth(Node head, int data, int position) {
Node node = new Node(data);
node.next = null;
if (head == null && position == 0){
head = node;
}
else if(head != null && position == 0){
node.next = head;
head = node;
}
Node tempCurrentNode = head ;
Node tempPreviousNode = null;
int index = 0;
while (index < position) {
tempPreviousNode = tempCurrentNode;
tempCurrentNode = tempCurrentNode.next;
index = index + 1;
}
node.next = tempCurrentNode;
tempPreviousNode.next = node;
return head;
}
Solution 3:[3]
var head;
class Node{
constructor(val) {
this.data=val;
this.next=null;
}
}
const sortedAdd = val => {
new_node = new Node(val);
var current
if(head == null || head.data > new_node.data) {
new_node.next = head;
head = new_node;
} else {
current = head;
while (current.next != null && current.next.data < new_node.data) {
current = current.next;
}
new_node.next = current.next;
current.next = new_node;
}
}
const add = val => {
new_node = new Node(val);
var current
if(head == null) {
head = new_node;
} else {
current = head;
while (current.next != null ) {
current = current.next;
}
current.next = new_node;
}
}
const insertAtPos= (val, insertIndex) => {
new_node = new Node(val);
var current, prev;
if(head == null) {
head = new_node;
} else {
current = head;
let index=0;
prev =current;
while (index <= insertIndex) {
if(0 === insertIndex) {
new_node.next = current;
prev.next = new_node;
break;
} else if(index === insertIndex) {
new_node.next = current;
prev.next = new_node;
break;
}
prev = current;
current = current.next;
index++;
}
}
}
const deleteNode = val =>{
var current = head;
var prev;
while(current != null) {
if(current.data === val) {
console.log('Deleted node ', current.data);
prev.next = current.next;
}
prev = current;
current = current.next;
}
}
const printList = () => {
let temp = head;
while(temp != null) {
console.log(temp.data);
temp = temp.next;
}
}
[5, 10, 7, 3, 1, 9].map(d => add(d));
console.log('Before');
printList();
console.log('After ');
insertAtPos(4, 3);
// deleteNode(3);
printList();
//output
Before 5 10 7 3 1 9 After 5 10 7 4 3 1 9
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Ashutosh Mishra |
| Solution 3 | Amol Gaikwad |
