How it works
Insert a new node at the head of a singly linked list by relinking the head pointer.
Implementation
function sllInsertHeadOp(state, value) { const valueArr = state.value; const next = state.next; const id = valueArr.length; valueArr[id] = value; next[id] = state.head; insertNode(id, value); if (state.head !== -1) { linkNodes(id, state.head); } state.head = id; finish(id); }
def sllInsertHeadOp(state, value): valueArr = state.value next = state.next id = valueArr.length valueArr[id] = value next[id] = state.head insertNode(id, value) if (state.head != -1): linkNodes(id, state.head) state.head = id finish(id)
#include <vector> #include <algorithm> void sllInsertHeadOp(int state, int value) { auto valueArr = state.value; auto next = state.next; auto id = valueArr.length; valueArr[id] = value; next[id] = state.head; insertNode(id, value); if((state.head != -1)) { linkNodes(id, state.head); } state.head = id; finish(id); }
public void sllInsertHeadOp(int state, int value) { var valueArr = state.value; var next = state.next; var id = valueArr.length; valueArr[id] = value; next[id] = state.head; insertNode(id, value); if((state.head != -1)) { linkNodes(id, state.head); } state.head = id; finish(id); }
#include <stdio.h> void sllInsertHeadOp(int state, int value) { var valueArr = state.value; var next = state.next; var id = valueArr.length; valueArr[id] = value; next[id] = state.head; insertNode(id, value); if((state.head != -1)) { linkNodes(id, state.head); } state.head = id; finish(id); }