---
# System prepended metadata

title: Linked List (insert)

---

# Linked List (insert)

## insert at head empty
```graphviz
digraph InsertAtHeadEmpty {
    rankdir=LR;
    node [shape=record, style=filled, fillcolor=lightgray];
    h [shape=plaintext, label="h"];
    null [label="null", shape=plaintext];
    // Before
    h -> null [label="before"];
    // Define node p (new node)
    p [label="{ (new)p | <link> link }"];
    // After
    h -> p [style=dashed, color=red, label="after"];
    p:link -> null [style=dashed, color=red];
}
```

## insert at head
```graphviz
digraph InsertAtHead {
    rankdir=LR;
    node [shape=record, style=filled, fillcolor=lightgray];
    h [shape=plaintext, label="h"];
    A [label="{ A | <link> link }"];
    B [label="{ B | <link> link }"];
    C [label="{ C | <link> link }"];
    null [label="null", shape=plaintext];
    
    // Before insertion
    h -> A [label="before"];
    A:link -> B;
    B:link -> C;
    C:link -> null;

    // Define node p (new node)
    p [label="{ (new)p | <link> link }"];
    // After insertion
    p:link -> A [style=dashed, color=red];
    h -> p [style=dashed, color=red label="after"];
}
```

## insert tail
```graphviz
digraph InsertAtTail {
    rankdir=LR;
    node [shape=record, style=filled, fillcolor=lightgray];
    h [shape=plaintext, label="h"];
    A [label="{ A | <link> link }"];
    B [label="{ B | <link> link }"];
    C [label="{ C | <link> link }"];
    null [label="null", shape=plaintext];
    
    // before
    h -> A;
    A:link -> B;
    B:link -> C;
    C:link -> null [label="before"];

    // Define node p (new node)
    p [label="{ (new)p | <link> link }"];
    p:link -> null[style=dashed, color=red];
    C:link -> p [style=dashed, color=red, label="after"];
}
```

## insert middle
```graphviz
digraph InsertAtMiddle {
    rankdir=LR;
    node [shape=record, style=filled, fillcolor=lightgray];
    h [shape=plaintext, label="h"];
    A [label="{ A | <link> link }"];
    B [label="{ B | <link> link }"];
    C [label="{ C | <link> link }"];
    null [label="null", shape=plaintext];

    // before
    h -> A;
    A:link -> B;
    B:link -> C [label="before"];
    C:link -> null;
    
    // workning poinger
    t [label="t", shape=plaintext];
    t -> B [style=dahsed, color=red];
    
    // after
    p [label="{ (new)p | <link> link }"];
    p:link -> C [style=dashed, color=red];
    B:link -> p [style=dashed, label="after" color=red ];
}
```
