forked from omonimus1/geeks-for-geeks-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-list-insert.cpp
More file actions
36 lines (31 loc) · 861 Bytes
/
Copy pathlinked-list-insert.cpp
File metadata and controls
36 lines (31 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// https://practice.geeksforgeeks.org/problems/linked-list-insertion-1587115620/1/?track=PC-W5-LL&batchId=154/*Structure of the linked list node is as
struct Node {
int data;
struct Node * next;
Node(int x) {
data = x;
next = NULL;
}
}; */
// function inserts the data in front of the list
Node *insertAtBegining(Node *head, int newData) {
Node *new_node = new Node(newData);
if(head == NULL)
return new_node;
else
{
new_node ->next = head;
return new_node;
}
}
// function appends the data at the end of the list
Node *insertAtEnd(Node *head, int newData) {
Node *new_node = new Node(newData);
if(head == NULL)
return new_node;
Node *current = head;
while(current->next != NULL)
current = current->next;
current->next = new_node;
return head;
}