Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
7aeabc9
Create README.md
sameera702 Dec 17, 2023
cffed7f
Create README.md
sameera702 Dec 17, 2023
79f753d
Create README.md
sameera702 Dec 17, 2023
fe04665
Create README.md
sameera702 Dec 17, 2023
16e8d04
Create README.md
sameera702 Dec 17, 2023
eccfe47
Create Addsll.py
sameera702 Dec 17, 2023
9dbb691
Create Addsll.cpp
sameera702 Dec 17, 2023
cf1a2f4
Create Addsll.java
sameera702 Dec 17, 2023
39cdbf5
Create bubblesort.py
sameera702 Dec 17, 2023
bb47612
Create bubblesort.cpp
sameera702 Dec 17, 2023
f03e166
Create bubblesort.java
sameera702 Dec 17, 2023
d083328
Create insertionsort.py
sameera702 Dec 17, 2023
20bf28e
Create insertionsort.cpp
sameera702 Dec 17, 2023
d1a7702
Create insertionsort.java
sameera702 Dec 17, 2023
bd85c3b
Create radixsort.py
sameera702 Dec 17, 2023
2e58bb7
Create radixsort.cpp
sameera702 Dec 17, 2023
ea6e3c0
Create radixsort.java
sameera702 Dec 17, 2023
6ef6f44
Create shellsort.py
sameera702 Dec 17, 2023
c3d291b
Create shellsort.cpp
sameera702 Dec 17, 2023
138c525
Create shellsort.java
sameera702 Dec 17, 2023
13e0fea
Update and rename Addsll.java to Linkedlistsum.java
sameera702 Dec 28, 2023
812a9c8
Update bubblesort.java
sameera702 Dec 28, 2023
a3c6392
Update insertionsort.java
sameera702 Dec 28, 2023
1e834e9
Update radixsort.java
sameera702 Dec 28, 2023
823154d
Update shellsort.java
sameera702 Dec 28, 2023
c70753a
Update README.md
sameera702 Dec 28, 2023
8e0aebb
Update README.md
sameera702 Dec 28, 2023
5a5b24b
Update README.md
sameera702 Dec 28, 2023
3e24413
Update README.md
sameera702 Dec 28, 2023
19fd6c4
Update README.md
sameera702 Dec 28, 2023
2164029
Update README.md
sameera702 Dec 28, 2023
f98bee0
Update README.md
sameera702 Dec 28, 2023
8ccfdb0
Update README.md
sameera702 Jan 3, 2024
ecc3a2c
Update Linkedlistsum.java
sameera702 Jan 3, 2024
11510b7
Update Addsll.py
sameera702 Jan 3, 2024
f2e8b5f
Update Addsll.cpp
sameera702 Jan 3, 2024
d83ffdd
Update README.md
sameera702 Jan 3, 2024
0da8dc1
Update bubblesort.cpp
sameera702 Jan 3, 2024
d848ea0
Update bubblesort.java
sameera702 Jan 3, 2024
ae5230e
Update bubblesort.py
sameera702 Jan 3, 2024
cd32962
Rename bubblesort.java to bubbleSort.java
sameera702 Jan 3, 2024
b263647
Update README.md
sameera702 Jan 3, 2024
786fe6d
Update and rename insertionsort.java to InsertionSort.java
sameera702 Jan 3, 2024
8f61131
Update insertionsort.cpp
sameera702 Jan 3, 2024
6571bcd
Update insertionsort.py
sameera702 Jan 3, 2024
71f3576
Update README.md
sameera702 Jan 3, 2024
8b7ff97
Update radixsort.cpp
sameera702 Jan 3, 2024
fbfd782
Update and rename radixsort.java to RadixSort.java
sameera702 Jan 3, 2024
ae4e3dd
Update radixsort.py
sameera702 Jan 3, 2024
4ace7b7
Update README.md
sameera702 Jan 3, 2024
06b10de
Update and rename shellsort.java to ShellSort.java
sameera702 Jan 3, 2024
2364402
Update shellsort.cpp
sameera702 Jan 3, 2024
ae883e6
Update shellsort.py
sameera702 Jan 3, 2024
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
116 changes: 116 additions & 0 deletions Linked List/Hard/Add two numbers in linked list/Addsll.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*Copyrights to venkys.io
For more information, visite https://venkys.io"/

C++ program to add two numbers in a linked list*/// Stable : Yes
// Inplace : Yes
// Adaptive : Yes

// Space complexity: (max(N, M)), where N and M are the lengths of the input linked lists.
//The extra space is used to store the resulting linked list.

//Time Complexity:O(max(N, M)), where N and M are the lengths of the input linked lists.

#include <iostream> // It is a standard C++ header file that includes declarations for the standard input/output stream objects like cout, cin, etc.
#include <vector> // Includes the Standard Template Library (STL) vector header for dynamic arrays.
#include <limits> // Include the header for numeric_limits

using namespace std; // Using the standard namespace for simplifying code

// Node class representing a node in a linked list
class Node {
public:
int data;
Node* next = NULL; // Pointer to the next node in the linked list, initially set to NULL

// Constructor to initialize a node with the given value
Node(int val) {
data = val;
}
};

//Function to print the linked list in reverse without arrows
void printReverse(Node* head){
// Base case: If the current node is NULL (end of the list), return
if (head==NULL){
return;
}
// Recursive call: Move to the next node in the list
printReverse(head->next);
// Print the data of the current node
cout << head->data << "";
}

/*// Function to print the linked list
void print(Node* head) {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << "->";
temp = temp->next;
}
cout << endl;
}
*/

// Function to build a linked list with a single node
Node* buildSingleNode(int val) {
return new Node(val);
}

// Function to add two numbers represented as linked lists
Node* addTwoNumbers(Node* l1, Node* l2) {
// Create a temporary node to serve as the dummy head of the result linked list
Node* temp = new Node(0);
// Pointer to the head of the result linked list
Node* head = temp;
// Variable to store the carry during addition
int c = 0;

// Loop until there are elements in either l1 or l2, or there is a carry
while (l1 != NULL || l2 != NULL || c != 0) {
// Add corresponding digits from l1 and l2, along with the carry
if (l1 != NULL) {
c += l1->data;
l1 = l1->next;
}
if (l2 != NULL) {
c += l2->data;
l2 = l2->next;
}

// Create a new node with the sum % 10 and update carry
temp->next = new Node(c % 10);
c = c / 10;
temp = temp->next;
}
// Return the result linked list starting from the second node (as the first node is the dummy head)
return head->next;
}

int main() {
int num1, num2;

// Input the first number
cout << "Enter the first number: ";
cin >> num1;

// Input the second number
cout << "Enter the second number: ";
cin >> num2;

// Create linked lists with a single node for each number
Node* l1 = buildSingleNode(num1);
Node* l2 = buildSingleNode(num2);

// Add the two numbers and print the result
Node* ans = addTwoNumbers(l1, l2);
cout << "Sum of the two numbers: ";
printReverse(ans);
cout << endl;

// Cleanup: Release memory
delete l1;
delete l2;
delete ans;

return 0;
}
89 changes: 89 additions & 0 deletions Linked List/Hard/Add two numbers in linked list/Addsll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'''Copyrights to venkys.io
For more information, visit https://venkys.io"/

Python program to add two numbers in a linked list'''

# Stable : Yes
# Inplace : Yes
# Adaptive : Yes

# Space complexity: O(max(N,M)), where N and M are the lengths of input linked lists.

#Time Complexity: O(max(N,M)), where N and M are the lengths of input linked lists.

# Constructor method for the Node class
class Node:
def __init__(self,data):
self.data=data # Store the provided data in the 'data' attribute of the node
self.next=None # Initialize the 'next' attribute to None, as this node is not yet connected to another node.

def printLLReverse(head):
#Function to print linked list in reverse order
stack=[]
# Traverse the linked list and push each node's data onto the stack
while head:
stack.append(head.data)
head=head.next
# Pop each element from the stack and print it in reverse order
while stack:
print(stack.pop(),end="")
# Print a newline to separate the reversed data from the rest of the output
print()

'''def printLL(head):
#Function to print the linked list
while head:
print(head.data,end="-->")
head=head.next
print()'''

def buildLL(arr):
#Function to build a linked list from an array
temp=Node(0)
head=temp
for i in arr:
# Create a new node with the current array element
temp.next=Node(i)
# Move the temporary pointer to the newly created node
temp=temp.next
return head.next

def addTwoNumbers(l1,l2):
#Function to add two numbers represented as linked lists
temp=Node(0)
head=temp
c=0
# loop until there are elements in either l1,;2 or there is carry.
while l1 or l2 or c:
# add corresponding digits from l1,l2 along with carry
if l1:
c+=l1.data
l1=l1.next
if l2:
c+=l2.data
l2=l2.next
#Create a new node with sum%10 and update carry
temp.next=Node(c%10)
c=c//10
temp=temp.next
return head.next


def main():
# Prompt the user to enter the first number
print("Enter first number:", end=" ")
num1 = input()
l1 = buildLL([int(x) for x in num1.split()])

# Prompt the user to enter the second number
print("Enter Second number:", end=" ")
num2 = input()
l2 = buildLL([int(x) for x in num2.split()])

# Print the reverse of the linked list representing the sum (result)
print("Sum of two numbers: ", end=" ")
ans = addTwoNumbers(l1, l2)
printLLReverse(ans)

if __name__ == "__main__":
main()
117 changes: 117 additions & 0 deletions Linked List/Hard/Add two numbers in linked list/Linkedlistsum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*Copyrights to venkys.io
For more information, visit https://venkys.io"/

Java program to add two numbers in a linked list*/

// Stable : Yes
// Inplace : Yes
// Adaptive :Yes

// Space complexity: (max(N, M)), where N and M are the lengths of the input linked lists.
//The extra space is used to store the resulting linked list.

//Time Complexity:O(max(N, M)), where N and M are the lengths of the input linked lists.

import java.util.Scanner; //Importing scanner class from java.util package for user input

//Node class representing a node in linked list
class Node{
int data;
Node next;

//Constructor to initialize a node with given data
Node(int data){
this.data=data;
}
}

public class Linkedlistsum{
// Function to print the linked list in reverse order
static void printReverse(Node head) {
// Base case: If the current node is null, return
if (head == null) {
return;
}
// Recursive call: Move to the next node and print it in reverse order
printReverse(head.next);
// Print the data of the current node
System.out.print(head.data);
}

/* This function displays the actual data order stored in linked list
static void print(Node head){
Node temp=head;
while(temp!=null){
System.out.print(temp.data+"->");
temp=temp.next;
}
System.out.println();
}*/

// Function to build a linked list from an array
static Node buildll(int[] arr){
Node temp=new Node(0); // Create a dummy node to serve as the head of the linked list
Node head=temp; // Store the reference to the head of the linked list

for(int i=0;i<arr.length;i++){
temp.next=new Node(arr[i]); //Create a new node with array element and link it
temp=temp.next; // Move the temporary pointer to newly created node
}
return head.next; // Return the actual head of the linked list (skip the dummy node)
}

// Function to add two numbers represented as linked lists
static Node addTwoNumbers(Node l1,Node l2){
Node temp=new Node(0); //Create a dummy node
Node head=temp; // Keep a reference to the head of the linked list

int c=0; // Initialize the carry to 0
// Continue adding digits until both input linked lists are processed, and there is no carry left
while(l1!=null || l2!=null || c!=0){
// Add the current digits of l1, l2, and the carry
if(l1!=null){
c+=l1.data;
l1=l1.next;
}
if(l2!=null){
c+=l2.data;
l2=l2.next;
}
temp.next=new Node(c%10); //Create a new node with the digit sum and link it
c=c/10; //Update the carry for the next iteration
temp=temp.next; //Move the temporary Pointer to the newly created node
}
return head.next; //Return the head of the resulting linked list(skipping dummy node)

}

public static void main(String[] args) {
// Input for the first number
System.out.print("Enter first number:");
Scanner scanner = new Scanner(System.in);
String[] input1 = scanner.nextLine().split(" ");
int[] a1 = new int[input1.length];
for (int i = 0; i < input1.length; i++) {
a1[i] = Integer.parseInt(input1[i]);
}

// Input for the second number
System.out.print("Enter second number:");
String[] input2 = scanner.nextLine().split(" ");
int[] a2 = new int[input2.length];
for (int i = 0; i < input2.length; i++) {
a2[i] = Integer.parseInt(input2[i]);
}

// Build linked lists
Node l1=buildll(a1);
Node l2=buildll(a2);

Node ans=addTwoNumbers(l1, l2);
System.out.print("Sum of two numbers:");
printReverse(ans);
System.out.println();
// Close the scanner
scanner.close();
}
}
Loading