What will be the time taken to add an node at the end of linked list if is initially pointing to first node of the list?

Linked List MCQ : Time Complexity [Multiple Choice Questions]

admin2013-06-02T03:29:40+00:00

Consider the following Chart for Reference :
[table border="1"] Operation,Array,Singly Linked List
Read [any where], O[1] , O[n]
Add/Remove at end, O[1] , O[n]
Add/Remove in the interior , O[n], O[n]
Resize ,O[n] , N/A
Find By position, O[1] , O[n]
Find By target [value], O[n] , O[n]
[/table]

inserting a node at the end of a linked list

The new node will be added at the end of the linked list.



Linked List | Set 2 [Inserting a node]

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 the following representations of linked list.

C++




// A linked list node
class Node
{
public:
int data;
Node *next;
};
// This code is contributed by rathbhupendra
C




// A linked list node
struct Node
{
int data;
struct Node *next;
};
Java




// Linked List Class
class LinkedList
{
Node head; // head of list
/* Node Class */
class Node
{
int data;
Node next;
// Constructor to create a new node
Node[int d] {data = d; next = null; }
}
}
Python




# Node class
class Node:
# Function to initialize the node object
def __init__[self, data]:
self.data = data # Assign data
self.next = None # Initialize next as null
# Linked List class
class LinkedList:
# Function to initialize the Linked List object
def __init__[self]:
self.head = None
C#




/* Linked list Node*/
public class Node
{
public int data;
public Node next;
public Node[int d] {data = d; next = null; }
}
Javascript




// Linked List Class
var head; // head of list
/* Node Class */
class Node {
// Constructor to create a new node
constructor[d] {
this.data = d;
this.next = null;
}
}
// This code is contributed by todaysgaurav

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.

Linked List

  • Last Updated : 29 Sep, 2020

1234

Video liên quan

Bài mới nhất

Chủ Đề