# How to remove an item from a linked list in Julia?

**URL:** https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145
**Category:** General Usage
**Created:** [January 4, 2018, 1:54am UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145 "2018-01-04T01:54:36Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)
#### Post date: [January 4, 2018, 1:54am UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145/1 "2018-01-04T01:54:36Z")

</div>

Please help me how can we remove an item from a linked list.  
In the code below, I can make a list by:

```julia
julia> L = list(1,3,5,7,9)
list(1,3,5,7,9)

```

Then, how to remove the 3rd element from L?

```julia
type Node
    value::Number
    next::Node

    function Node()
        n = new() # point to itself
        n.next = n
    end 

    function Node(value::Number, next::Node) # a regular constructor
        new(value, next)
    end 
end

#"Add Elements to List"
function unshift(head::Node, values::Number...)

    for v in reverse(values)
        head = Node(v, head)
    end 
    head
end 

function list(values::Number...)

    tail = Node()
    unshift(tail, values...)
end 

# Implement Iteration Interface
Base.start(head::Node) = head
Base.next(head::Node, n::Node) = n.value, n.next
Base.done(head::Node, n::Node) = n == n.next

# Using join for pretty printing:
function Base.show(io::IO, n::Node)
   println(io, "list(", join(n, ",")")" )
end 

# Add value to a linked list
function Base.append!(l::Node, x::Number...) 

    for v in reverse(x)
        l = Node(v, l)
    end 
    l
end

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 4, 2018, 1:56am UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145/2 "2018-01-04T01:56:38Z")

</div>

Is this a homework question?

---

<div class="post-metadata">

### Author: ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)
#### Post date: [January 4, 2018, 2:14am UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145/3 "2018-01-04T02:14:42Z")

</div>

I am major in geotechnical engineering and currently studying Julia for my research in graduate school. It’s not a homework for any class. I don’t know how to properly use pointers in this case. Please tell me how to do it

---

<div class="post-metadata">

### Author: ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)
#### Post date: [January 4, 2018, 2:14am UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145/4 "2018-01-04T02:14:44Z")

</div>

It’s a self-study problem, not a homework for a class. I am grateful if you can help me understand more

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 4, 2018, 2:26am UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145/5 "2018-01-04T02:26:32Z")

</div>

Alright, to remove e.g. the third node, traverse to the second node (using, for example, the iteration interface you defined). Store that node. Then traverse to the fourth node. Store that node. Then set the `next` field of the second node to the fourth node.

---

<div class="post-metadata">

### Author: ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)
#### Post date: [January 4, 2018, 2:48am UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145/6 "2018-01-04T02:48:39Z")

</div>

Thank you very much for your help! I got the result:

```julia

function Base.delete!(l::Node, index::Int64)

      l_before = l
      l_after = l
      for i = 1:(index-2)
            l_before = l_before.next
      end

      for i = 1:(index)
          l_after = l_after.next
      end
     
      l_before.next = l_after

      return l
end

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 4, 2018, 3:03am UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145/7 "2018-01-04T03:03:26Z")

</div>

Since you have defined an iteration interface, it might be good to use that instead of directly accessing the `.next` fields.

You could also do slightly better than now, by not restarting your iteration from 1 in the second loop. Instead, you could just keep going forward two steps from `l_before`.

---

<div class="post-metadata">

### Author: ![QUANG\_VU\_NGUY\_N\_PH\_M](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quang_vu_nguy_n_ph_m/32/14299_2.png) [@QUANG\_VU\_NGUY\_N\_PH\_M](https://discourse.julialang.org/u/QUANG_VU_NGUY_N_PH_M)
#### Post date: [January 4, 2018, 3:24am UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145/8 "2018-01-04T03:24:46Z")

</div>

```julia
function Base.delete!(l::Node, index::Int64)

    l_before = l
    for i = 1:(index-2)
        l_before = l_before.next
    end
    l_before.next = l_before.next.next
    return l
end

```

Thank for your help! I understand your second idea and implemented it like the above. But how about the first idea? I dont know how to use that iteration interface as you mentioned

---

<div class="post-metadata">

### Author: ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)
#### Post date: [January 5, 2018, 6:50pm UTC](https://discourse.julialang.org/t/how-to-remove-an-item-from-a-linked-list-in-julia/8145/9 "2018-01-05T18:50:58Z")

</div>

Something like

```julia
for (i,node) in enumerate(l)
  if i==index+1 
      lastnode.next=node.next
      break
  end
  lastnode=node
end

```

Though I don’t think that handles deleting at `index=1`.
