-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesignLinkedList.cpp
92 lines (88 loc) · 2.57 KB
/
DesignLinkedList.cpp
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
#include<bits/stdc++.h>
using namespace std ;
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* solve(vector<vector<int> > &A) {
ListNode* Head = NULL ;
int SizeofLL = 0 ;
for(int i = 0 ; i < A.size() ; i++){
if(A[i][0] == 0 ){
if(Head == NULL){
Head = new ListNode(A[i][1]);
}
else{
ListNode* temp = new ListNode(A[i][1]) ;
temp -> next = Head ;
Head = temp ;
}
SizeofLL++;
}
else if(A[i][0] == 1){
if(Head == NULL){
Head = new ListNode(A[i][1]);
}
else{
ListNode* temp = new ListNode(A[i][1]) ;
ListNode* curr = Head ;
while(curr -> next != NULL){
curr = curr -> next ;
}
curr -> next = temp ;
}
SizeofLL++;
}
else if(A[i][0] == 2){
int index = A[i][2];
int value = A[i][1];
if(index <= SizeofLL || index > -1 ){
ListNode * temp = new ListNode(value);
if(index == 0){
temp -> next = Head ;
Head = temp ;
SizeofLL++;
continue;
}
ListNode * curr = Head ;
for(int i = 1 ; i < index ; i++ ){
curr = curr -> next ;
}
temp -> next = curr -> next ;
curr -> next = temp ;
SizeofLL++;
}
}
else{
int index = A[i][1];
if(SizeofLL == 0){
continue ;
}
if(index < SizeofLL || index > -1 ){
if(index == 0){
Head = Head -> next ;
SizeofLL--;
continue ;
}
ListNode * curr = Head ;
for(int i = 1 ; i < index ; i++ ){
curr = curr -> next ;
}
ListNode* temp = curr -> next ;
curr -> next = curr -> next -> next ;
delete(temp);
SizeofLL--;
}
}
}
return Head ;
}