How it works
Append a new node to the tail of a singly linked list by traversing to the final node.
Implementation
function sllInsertTailOp(state, value) { const valueArr = state.value; const next = state.next; const id = valueArr.length; valueArr[id] = value; next[id] = -1; insertNode(id, value); if (state.head === -1) { state.head = id; finish(id); return; } let current = state.head; while (next[current] !== -1) { visitNode(current); current = next[current]; } next[current] = id; linkNodes(current, id); finish(id); }
def sllInsertTailOp(state, value): valueArr = state.value next = state.next id = valueArr.length valueArr[id] = value next[id] = -1 insertNode(id, value) if (state.head == -1): state.head = id finish(id) return current = state.head while (next[current] != -1): visitNode(current) current = next[current] next[current] = id linkNodes(current, id) finish(id)
#include <vector> #include <algorithm> void sllInsertTailOp(int state, int value) { auto valueArr = state.value; auto next = state.next; auto id = valueArr.length; valueArr[id] = value; next[id] = -1; insertNode(id, value); if((state.head == -1)) { state.head = id; finish(id); return; } auto current = state.head; while((next[current] != -1)) { visitNode(current); current = next[current]; } next[current] = id; linkNodes(current, id); finish(id); }
public void sllInsertTailOp(int state, int value) { var valueArr = state.value; var next = state.next; var id = valueArr.length; valueArr[id] = value; next[id] = -1; insertNode(id, value); if((state.head == -1)) { state.head = id; finish(id); return; } var current = state.head; while((next[current] != -1)) { visitNode(current); current = next[current]; } next[current] = id; linkNodes(current, id); finish(id); }
#include <stdio.h> void sllInsertTailOp(int state, int value) { var valueArr = state.value; var next = state.next; var id = valueArr.length; valueArr[id] = value; next[id] = -1; insertNode(id, value); if((state.head == -1)) { state.head = id; finish(id); return; } var current = state.head; while((next[current] != -1)) { visitNode(current); current = next[current]; } next[current] = id; linkNodes(current, id); finish(id); }