-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
74 lines (67 loc) · 1.32 KB
/
Copy pathLinkedList.cpp
File metadata and controls
74 lines (67 loc) · 1.32 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
#include<bits/stdc++.h>
using namespace std;
class LinkedList {
public:
int value;
LinkedList* next;
LinkedList(int x) {
value = x;
next = NULL;
}
};
void Display(LinkedList* head) {
while(head) {
cout << head->value << " ";
head = head->next;
}
cout << "\n";
}
LinkedList* createNode(LinkedList* head, int number) {
if(head == NULL) return new LinkedList(number);
LinkedList* temp = head;
while(temp->next)
temp = temp->next;
temp->next = new LinkedList(number);
return head;
}
LinkedList* reverseList(LinkedList* head) {
LinkedList* current = head;
LinkedList* temp = head;
LinkedList* previous = NULL;
while(current) {
current = current->next;
temp->next = previous;
previous = temp;
temp = current;
}
return previous;
}
LinkedList* deleteNode(LinkedList* head, int number) {
LinkedList* previous = NULL;
LinkedList* temp = head;
while(temp) {
if(temp->value == number) break;
previous = temp;
temp = temp->next;
}
if(temp == NULL) {
cout << "Node not found" << endl;
return head;
}
if(previous == NULL) return head->next;
previous->next = temp->next;
return head;
}
int main() {
LinkedList* head = NULL;
int number;
cin >> number;
while(number != -1) {
head = createNode(head, number);
cin >> number;
}
Display(head);
head = reverseList(head);
Display(head);
return 0;
}