diff --git a/cpp/_2.cpp b/cpp/_2.cpp new file mode 100644 index 0000000000..461a6c89ef --- /dev/null +++ b/cpp/_2.cpp @@ -0,0 +1,64 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { + int carry =0; + ListNode* head = NULL; + ListNode* last = NULL; + while(l1&&l2){ + int temp = l1->val+l2->val+carry; + ListNode* cur = new ListNode(temp%10); + carry = temp/10; + if(head == NULL){ + head = cur; + last = cur; + }else{ + last->next = cur; + last = cur; + } + l1=l1->next; + l2=l2->next; + } + while(l1){ + int temp = l1->val+carry; + ListNode* cur = new ListNode(temp%10); + carry = temp/10; + if(head == NULL){ + head = cur; + last = cur; + }else{ + last->next = cur; + last = cur; + } + l1=l1->next; + } + while(l2){ + int temp = l2->val+carry; + ListNode* cur = new ListNode(temp%10); + carry = temp/10; + if(head == NULL){ + head = cur; + last = cur; + }else{ + last->next = cur; + last = cur; + } + l2=l2->next; + } + if(carry){ + ListNode* cur = new ListNode(carry); + last->next = cur; + cur->next= NULL; + } + return head; + } +}; \ No newline at end of file diff --git a/cpp/_5.cpp b/cpp/_5.cpp new file mode 100644 index 0000000000..a4505084c3 --- /dev/null +++ b/cpp/_5.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + vector >dp; + int ispalin(int i,int j,string &s){ + if(i>j||i==j)return 1; + if(dp[i][j]!=-1)return dp[i][j]; + + if(s[i]==s[j]){ + return dp[i][j]=ispalin(i+1,j-1,s); + } + return dp[i][j]=0; + } + string longestPalindrome(string s) { + int n = s.length(); + int jf=0;int af=0; + dp.resize(n,vector(n,-1)); + for(int i =0;i