Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions C++/AddTwoNumbersInLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Problem statement:-
* You are given two non-empty linked lists representing two non-negative integers.
* The digits are stored in reverse order, and each of their nodes contains a single digit.
* Add the two numbers and return the sum as a linked list.
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
* 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:
string findSum(string str1, string str2)
{
if (str1.length() > str2.length())
swap(str1, str2);
string str = "";
long long int n1 = str1.length(), n2 = str2.length();
long long int carry = 0;
for (long long int i=0; i<n1; i++)
{
long long int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);
str.push_back(sum%10 + '0');

carry = sum/10;
}
for (long long int i=n1; i<n2; i++)
{
long long int sum = ((str2[i]-'0')+carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}
if (carry)
str.push_back(carry+'0');
//reverse(str.begin(), str.end());
return str;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
string s1,s2,ans;
ListNode *temp=l1;
while(temp!=nullptr){
s1+=(temp->val)+'0';
temp=temp->next;
}
temp=l2;
while(temp!=nullptr){
s2+=(temp->val)+'0';
temp=temp->next;
}
ans=findSum(s1,s2);
ListNode *start=nullptr,*prev;
for(long long int i=0;i<ans.size();i++){
ListNode *n=new ListNode;
n->val=(ans[i]-'0');
n->next=nullptr;
if(start==nullptr){
start=n;
}
else{
prev->next=n;
}
prev=n;
}
return start;
}
};
44 changes: 19 additions & 25 deletions JAVA/bubbleSort.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
import java.util.Arrays;

public class bubbleSort {

// bubble sort:
// Bubble sort is an algorithm where you can sort an array by making pairs and comparing between them and through it the largest elemnt in the array get sorted first.

// Time complexity: O(n^2) in both best and worst case.

// Unique Feature: Large values are always sorted first. It only takes one iteration to detect that a collection is already sorted. The best time complexity for Bubble Sort is O(n).

public static void bubbleSort(int[] arr){
for(int i=0; i<arr.length-1; i++){
for(int j=0; j<arr.length-1-i; j++){
int temp= arr[j];
if(arr[j+1]<arr[j]){
arr[j]= arr[j+1];
arr[j+1]= temp;
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;

for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

}

public static void main(String[] args) {
int[] a= {1, 5, 6, 8, 10, 4, 12, 3, 0, 1};

bubbleSort(a);
for(int i=0; i<a.length;i++){
System.out.print(a[i]+" ");
}
int[] arr = {5, 1, 4, 2, 8};

System.out.println("Original array:");
for (int num : arr) System.out.print(num + " ");

bubbleSort(arr);

System.out.println("\nSorted array:");
for (int num : arr) System.out.print(num + " ");
}
}