# Data Structures and Algo

**URL:** <https://forum.freecodecamp.org/t/data-structures-and-algo/481227>\
**Category:** General\
**Created:** [October 11, 2021, 5:41am UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227 "2021-10-11T05:41:48Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![catalanjhonlyodm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalanjhonlyodm/32/201392_2.png) [@catalanjhonlyodm](https://forum.freecodecamp.org/u/catalanjhonlyodm)\
**Post date:** [October 11, 2021, 5:41am UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227/1 "2021-10-11T05:41:48Z")

</div>

Hello everyone!  
How can I add a node before in a given node in Singly Linked list.  
Thank you!

I am using java, and I have created the a snippet on how to add After a node like this

```auto
public voided addAfter(int target value, int data) {

Node tmp;
for (tmp=head; tmp.data!=target value && tmp.next!=null; tmp = tmp.next) {
}
Node nextOfTmp = tmp.next;
tmp.next = new Node(data, nextOfTmp);
}

```

That is what I have coded for addAfter and now I am troubled if how Am I supposed to add Before a given node

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [October 11, 2021, 5:48am UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227/2 "2021-10-11T05:48:37Z")

</div>

Do you understand the concept of a linked list? If so it should be pretty easy to visualize the algorithm for doing this. As to how to code this, that would depend on the language and how you are implementing the list. You would need to give us more specifics. Preferably, give us the code that you have so far so we can see what you have tried and then help you get the result you want.

---

<div class="post-metadata">

**Author:** ![catalanjhonlyodm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalanjhonlyodm/32/201392_2.png) [@catalanjhonlyodm](https://forum.freecodecamp.org/u/catalanjhonlyodm)\
**Post date:** [October 11, 2021, 6:28am UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227/3 "2021-10-11T06:28:20Z")

</div>

I am using java, and I have created the a snippet on how to addAfter a node like this

```auto
public voided addAfter(int target value, int data) {

Node tmp;
for (tmp=head; tmp.data!=target value && tmp.next!=null; tmp = tmp.next) {
}
Node nextOfTmp = tmp.next;
tmp.next = new Node(data, nextOfTmp);
}

```

That is what I have coded for addAfter and now I am troubled if how Am I supposed to add Before a given node

---

<div class="post-metadata">

**Author:** ![catalanjhonlyodm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalanjhonlyodm/32/201392_2.png) [@catalanjhonlyodm](https://forum.freecodecamp.org/u/catalanjhonlyodm)\
**Post date:** [October 11, 2021, 6:52am UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227/4 "2021-10-11T06:52:13Z")

</div>

I am using java, and I have created the a snippet on how to addAfter a node like this

```auto
public voided addAfter(int target value, int data) {

Node tmp;
for (tmp=head; tmp.data!=target value && tmp.next!=null; tmp = tmp.next) {
}
Node nextOfTmp = tmp.next;
tmp.next = new Node(data, nextOfTmp);
}

```

That is what I have coded for addAfter and now I am troubled if how Am I supposed to add Before a given node

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [October 11, 2021, 7:20am UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227/5 "2021-10-11T07:20:07Z")

</div>

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![Jagaya](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jagaya/32/202277_2.png) [@Jagaya](https://forum.freecodecamp.org/u/Jagaya)\
**Post date:** [October 11, 2021, 9:59am UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227/6 "2021-10-11T09:59:21Z")

</div>

Because you cannot go backwards in a single-LL, you have to keep track of the currentNode and the previousNode. Once the currentNode has your target value, you insert the newNode inbetween.

---

<div class="post-metadata">

**Author:** ![catalanjhonlyodm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalanjhonlyodm/32/201392_2.png) [@catalanjhonlyodm](https://forum.freecodecamp.org/u/catalanjhonlyodm)\
**Post date:** [October 11, 2021, 10:25am UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227/7 "2021-10-11T10:25:30Z")

</div>

Thank you! very much for your help!

---

<div class="post-metadata">

**Author:** ![catalanjhonlyodm](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalanjhonlyodm/32/201392_2.png) [@catalanjhonlyodm](https://forum.freecodecamp.org/u/catalanjhonlyodm)\
**Post date:** [October 11, 2021, 10:26am UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227/8 "2021-10-11T10:26:17Z")

</div>

Thank you very much! for your help

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [April 11, 2022, 10:27pm UTC](https://forum.freecodecamp.org/t/data-structures-and-algo/481227/9 "2022-04-11T22:27:01Z")

</div>

Closed on April 11, 2022, 10:27pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
