File tree Expand file tree Collapse file tree 4 files changed +146
-0
lines changed
005 - Linked List/Practice 01 Expand file tree Collapse file tree 4 files changed +146
-0
lines changed Original file line number Diff line number Diff line change @@ -527,6 +527,45 @@ Node* kReverse(Node* head,int k){
527527return vishal;
528528}
529529
530+ // Next Number
531+ Node* nextNumber (Node* head){
532+ if (head==NULL ){
533+ return NULL ;
534+ }
535+ Node *temp = head;
536+ int count = 1 ;
537+ while (temp->next !=NULL ){
538+ temp = temp->next ;
539+ count++;
540+ }
541+ if (temp->data != 9 ){
542+ temp->data = (temp->data )+1 ;
543+ return head;
544+ }
545+ Node *amit = head;
546+ int sum = 0 ;
547+ for (int i=count-1 ;i>=0 ;i--){
548+ sum = sum*10 + (amit->data );
549+ amit = amit->next ;
550+ }
551+ sum = sum+1 ;
552+ Node *yash_head = NULL ;
553+ Node *yash_tail = NULL ;
554+ while (sum>0 ){
555+ Node *vishal = new Node (sum%10 );
556+ if (yash_head==NULL ){
557+ yash_head = vishal;
558+ yash_tail = vishal;
559+ }else {
560+ yash_tail->next = vishal;
561+ yash_tail = yash_tail->next ;
562+ }
563+ sum/=10 ;
564+ }
565+ return yash_head;
566+ }
567+
568+
530569
531570int main (){
532571// 10 20 30 40 50 60 70 80 90 100
@@ -554,5 +593,8 @@ int main(){
554593// printLinkedList(skipMdeleteN(head,2,2));
555594// printLinkedList(swapTwoNodes(head,2,4));
556595// printLinkedList(kReverse(head,3));
596+
597+
598+ printLinkedList (deleteAlternative (head));
557599return 0 ;
558600}
Original file line number Diff line number Diff line change 1+ #include < stack>
2+ int countBracketReversals (string s) {
3+ // Write your code here
4+ if (s.length ()%2 !=0 ){
5+ return -1 ;
6+ }
7+ stack<char > st;
8+ for (int i=0 ;i<s.length ();i++){
9+ if (s[i]==' }' &&st.size ()!=0 &&st.top ()==' {' ){
10+ st.pop ();
11+ }else {
12+ st.push (s[i]);
13+ }
14+
15+ }
16+ char c1,c2;
17+ int count=0 ;
18+ while (st.size ()!=0 ){
19+ c1=st.top ();
20+ st.pop ();
21+ c2=st.top ();
22+ st.pop ();
23+ if (c1==c2){
24+ count++;
25+ }else if (c1==' {' ){
26+ count+=2 ;
27+ }
28+ }
29+ return count;
30+
31+ }
Original file line number Diff line number Diff line change @@ -63,6 +63,58 @@ bool redundantBracket(string s){
6363return false ;
6464}
6565
66+ // Stock Span Function
67+ int * stockSpan (int * price,int size){
68+ int * data=new int [size];
69+ stack<int > st;
70+ st.push (0 );
71+ data[0 ]=1 ;
72+ for (int i=1 ;i<size;i++){
73+ while (st.size ()!=0 && price[i]>price[st.top ()]){
74+ st.pop ();
75+ }
76+ if (st.size ()==0 ){
77+ data[i]=i+1 ;
78+ }else {
79+ data[i]=i-st.top ();
80+ }
81+ st.push (i);
82+ }
83+ return data;
84+ }
85+
86+ // Minimum Brackets Reversal
87+ int minBracketRev (string s){
88+ if (s.length ()%2 !=0 ){
89+ return -1 ;
90+ }
91+ stack<char > st;
92+ for (int i=0 ;i<s.length ();i++){
93+ if (s[i]==' }' &&st.size ()!=0 &&st.top ()==' {' ){
94+ st.pop ();
95+ }else {
96+ st.push (s[i]);
97+ }
98+
99+ }
100+ char c1,c2;
101+ int count=0 ;
102+ while (st.size ()!=0 ){
103+ c1=st.top ();
104+ st.pop ();
105+ c2=st.top ();
106+ st.pop ();
107+ if (c1==c2){
108+ count++;
109+ }else {
110+ if (c1==' }' ){
111+ count+=2 ;
112+ }
113+ }
114+ }
115+ return count;
116+ }
117+
66118int main (){
67119
68120
@@ -132,5 +184,6 @@ int main(){
132184
133185// cout<<redundantBracket("(a+b)")<<endl;
134186
187+ cout<<minBracketRev (" {{{{}{" )<<endl;
135188
136189}
Original file line number Diff line number Diff line change 1+ #include < stack>
2+ int * stockSpan (int *price, int size) {
3+ // Write your code here
4+ stack<int > st;
5+ int *ans = new int [size];
6+ ans[0 ]=1 ;
7+ st.push (0 );
8+ for (int i=1 ;i<size;i++){
9+ while (st.size ()!=0 && price[i]>price[st.top ()]){
10+ st.pop ();
11+ }
12+ if (st.size ()==0 ){
13+ ans[i]=i+1 ;
14+ }else {
15+ ans[i]=i-st.top ();
16+ }
17+ st.push (i);
18+ }
19+ return ans;
20+ }
You can’t perform that action at this time.
0 commit comments