Placement Preparation 
Linked List 
Shobhit Chaurasia 
B.Tech, CSE
Target Audience 
People who have coding experience limited to the basic 
syntax of C++, pointers and structures, but didn’t take 
CS101 seriously and want to learn some basics of coding 
quickly. 
If you already know how to implement a linked list, then 
please spare me the pain of taking a lecture on linked 
lists, and volunteer to deliver this lecture.
Motivation 
Suppose I have an array: 1,4,10,19,6 
I want to insert a 7 between the 4 and the 10 
What do we need to do?
Linked List 
head
Node
How to represent a Node in C++ ? 
How to represnt integers in C++? 
int x; 
But Node is a combination of data + pointer 
(int) + pointer 
(char) + pointer 
(string) + pointer
Structures Revisited 
Structure variable 
stud s; 
Access members of struct 
; 
s.rollNo = 11010179 
s.cpi = 8.1 
s.gender = ‘M’
How to represent Node in C++ 
;
Head 
Pointer to the first node. 
Holds the address of the first node in the list 
head
Head 
Pointer to the first node. 
node* head 
;
Struct Pointer revisited 
Struct Pointer 
stud *s; 
Access members of struct 
s->rollNo = 11010179 
s->cpi = 8.1 
s->gender = ‘M’ 
rollNo cpi gender 
s 
;
Accessing data/next node 
node* head 
Data stored at first node 
head->data = ‘a’; 
Pointer to second node 
head->next 
Data stored at second node 
(head->next)->data = ‘b’; 
head 
;
Demo #0(a) 
node* temp; 
temp = head; 
cout<<temp->data; 
head
Demo #0(a) 
How to go to the second node? 
temp = temp->next; 
cout<<temp->data; 
head
Demo #0(a) 
How to go to the third node? 
temp = temp->next; 
cout<<temp->data; 
head
Where’s the list’s end? 
head 
ptr
Demo #0(b) 
Printing the linked list
Demo #0(b) 
Printing the linked list (correct version)
Demo #0(b) 
An observation
Demo #0(b) - MORAL 
Never move the HEAD while traversing a 
linked list
Demo #1 
Function to print the 3rd element in linked list
Insert a new node at the end 
node* temp; 
temp = start; 
temp->next == NULL ??
Insert a new node at the end 
temp = temp->next; 
temp->next == NULL ??
Insert a new node at the end 
temp = temp->next; 
temp->next == NULL ??
Insert a new node at the end 
temp = temp->next; 
temp->next == NULL ??
Insert a new node at the end 
temp = temp->next; 
temp->next == NULL ?? (YES!! Finally reached the end)
Insert a new node at the end 
node* ptr = new node 
ptr
Insert a new node at the end 
ptr 
ptr->data = ‘f’; 
ptr->next = NULL; 
f
Insert a new node at the end 
ptr 
temp->next = ptr;
Demo #2 
Inserting a new node at the end. 
Discuss 2.cpp
Insert a new node at the start 
node* ptr = new node; 
ptr
Insert a new node at the start 
node* ptr = new node; 
ptr->data = ‘z’; 
z ptr
Insert a new node at the start 
ptr->next = start; 
ptr
Insert a new node at the start 
start = ptr; 
ptr
Demo #3 
Insert a new node at the head 
Discuss 3.cpp (two methods)
Demo #4 
Create a linked list and print its contents.
Demo #5 
Same as last problem. Use functions.
Insert after a particular node
Demo #6 
Write a function which takes as parameter a 
node pointer and inserts a new node after it. 
insertInBetween (node* temp)
Deleting first node
Deleting first node
ACTUALLY Deleting first node
ACTUALLY Deleting first node
ACTUALLY deleting first node
ACTUALLY deleting first node 
delete frees the memory allocated by new
Demo #7 
Write a function to delete the first node of a 
linked list. 
Add this function to 5.cpp
Demo #8 
Write a function to delete the last node of 
linked list 
Add this function to 7.cpp
Delete a node inside the linked list
Delete a node inside the linked list
Locating the node “y”
Can we delete the node “y” now? 
Having just the pointer to the node containing 
“y” sufficient to delete it?
What do we want to do? 
Break this link, and make it point to the 
node “d”
Expected Output
What do we need? 
Pointer to the previous node
Step 1 
prev->next = temp->next;
Step 2 
delete temp;
Final output
Demo #9 
Delete the node with data = 3 
Add this function to 5.cpp
Homework 
Reverse a linked list.
Linked list vs Arrays 
Operation Linked List Array 
Insert at start O(1) O(n) 
Insert anywhere O(1) O(n) 
Delete anywhere O(1) O(n) 
Access ith element O(n) O(1)
THANK YOU

Lecture 6: linked list