-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathdeleteNode.cpp
More file actions
111 lines (89 loc) · 2.13 KB
/
deleteNode.cpp
File metadata and controls
111 lines (89 loc) · 2.13 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Problem: Only given a pointer to node of the linked list, delete the node.
*/
#include <iostream>
struct Node {
int data;
Node * next;
Node( int d ) : data{ d }, next{ nullptr } { }
};
void insert( Node * & head, int data )
{
Node *newNode = new Node(data);
if ( head == nullptr ) {
head = newNode;
} else {
Node * temp = head;
while ( temp->next != nullptr ) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printList( Node * head )
{
while( head ) {
std::cout << head->data << "-->";
head = head->next;
}
std::cout << "NULL" << std::endl;
}
void deleteNode( Node * node )
{
// return if node is null
if ( node == nullptr ) {
return;
}
//this method won't work if we are given last node
if ( node->next == nullptr ) {
return;
}
Node * nextNode = node->next;
node->data = nextNode->data;
node->next = nextNode->next;
delete nextNode;
}
//Delete a Linked List node at a given position
void deleteNode(Node **head_ref, int position)
{
// If linked list is empty
if (*head_ref == NULL)
return;
// Store head node
Node* temp = *head_ref;
// If head needs to be removed
if (position == 0)
{
// Change head
*head_ref = temp->next;
// Free old head
free(temp);
return;
}
// Find previous node of the node to be deleted
for(int i = 0; temp != NULL && i < position - 1; i++)
temp = temp->next;
// If position is more than number of nodes
if (temp == NULL || temp->next == NULL)
return;
// Node temp->next is the node to be deleted
// Store pointer to the next of node to be deleted
Node *next = temp->next->next;
// Unlink the node from linked list
free(temp->next); // Free memory
temp->next = next;
}
int main()
{
Node * head = nullptr;
insert( head, 1 );
insert( head, 12 );
insert( head, 2 );
insert( head, 3 );
insert( head, 4 );
printList( head );
std::cout << "Deleting node with data 12 \n";
deleteNode( head->next );
printList( head );
return 0;
}