You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Furkan Türkal dc805fe272 update readme 7 years ago
..
resources update readme & added images 7 years ago
README.md update readme 7 years ago
linked-list-2-inserting-a-node.go updated and added find-length 7 years ago

README.md

LinkedList | SET 2 (INSERTING A NODE) Source

What It Is

What It Is

We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal. All programs discussed in this post consider following representations of linked list .

In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways ;

  • 1) At the front of the linked list
  • 2) After a given node.
  • 3) At the end of the linked list.

Add a node at the front (A 4 steps process)

The new node is always added before the head of the given Linked List. And newly added node becomes the new head of the Linked List. For example if the given Linked List is 10->15->20->25 and we add an item 5 at the front, then the Linked List becomes 5->10->15->20->25. Let us call the function that adds at the front of the list is push(). The push() must receive a pointer to the head pointer, because push must change the head pointer to point to the new node.

Preview Thumbnail

Algorithm Complexity

Complexity Notation
Time Complexity O(1)

Add a node after a given node (5 steps process)

We are given pointer to a node, and the new node is inserted after the given node.

Preview Thumbnail

Algorithm Complexity

Complexity Notation
Time Complexity O(1)

Add a node at the end (6 steps process)

The new node is always added after the last node of the given Linked List. For example if the given Linked List is 5->10->15->20->25 and we add an item 30 at the end, then the Linked List becomes 5->10->15->20->25->30. Since a Linked List is typically represented by the head of it, we have to traverse the list till end and then change the next of last node to new node.

Preview Thumbnail

Algorithm Complexity

Complexity Notation
Time Complexity O(n)