[英]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;
}
node = node.next;
prev = node;
This should be in the opposite order这应该是相反的顺序
prev = node;
node = node.next;
And also the code does not check for many cases , for example as to whether the position specified is larger than the size of the linked list.并且代码不会检查很多情况,例如指定的位置是否大于链表的大小。 Try rewriting the code , also could you mention which values are you using for testing the function.
尝试重写代码,您能否提及您使用哪些值来测试该功能。 I think
prev=node
should throw an exception as prev was not initialized我认为
prev=node
应该抛出异常,因为 prev 没有初始化
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;
}
}
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;
}
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之前 5 10 7 3 1 9 之后 5 10 7 4 3 1 9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.