diff --git a/DSA/Linked-List/__init__.py b/DSA/Linked-List/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/DSA/Linked-List/linked_list.py b/DSA/Linked-List/linked_list.py deleted file mode 100644 index 3c9f1ea..0000000 --- a/DSA/Linked-List/linked_list.py +++ /dev/null @@ -1,62 +0,0 @@ -# Node class -class Node: - - # Function to initialize the node object - def __init__(self, data): - self.data = data # Assign data - self.next = None # Initialize next as null - - -# Singly Linked List class -class LinkedList: - - # Function to initialize the Linked List object - def __init__(self): - self.head = None - - # Function to insert a new node at the beginning - def push(self, new_data): - - # create a new node with new data - new_node = Node(new_data) - # as we are pushing at the beginning we point next node toward - # like: new_node -> self.head(original node) - new_node.next = self.head - # now we can say new node as our original node - self.head = new_node - - def append(self, new_data): - # lets create a new node first - new_node = Node(new_data) - # case 1: if we have empty linked list, like our linked list is empty - # in that case we can initialize the value in the first position - if self.head is None: - print(f"Head is empty: self.head = {self.head}") - self.head = new_node - return - # case 2: our linked is not empty - # in this case we need to find the last element of the linked list - # as we want push at the end of the linked list - last = self.head - while last.next: - # keep looking for last item of the linked list - last = last.next - # while loop make sure that we have our last element - # now we can assign to next to our list element toward the new node - last.next = new_node - - def print_nodes(self): - result = [] - current_head = self.head - while current_head is not None: - result.append(current_head.data) - current_head = current_head.next - print(result) - - -linked_list = LinkedList() -linked_list.push("A") -linked_list.push("B") -linked_list.print_nodes() -linked_list.append("C") -linked_list.print_nodes() diff --git a/DSA/Linked-List/merge_two_list.py b/DSA/Linked-List/merge_two_list.py deleted file mode 100644 index cc5fffd..0000000 --- a/DSA/Linked-List/merge_two_list.py +++ /dev/null @@ -1,61 +0,0 @@ -from typing import Optional - - -# Definition for singly-linked list. -class ListNode: - def __init__(self, val=0, next=None): - self.val = val - self.next = next - - -class Solution: - def mergeTwoLists( - self, list1: Optional[ListNode], list2: Optional[ListNode] - ) -> Optional[ListNode]: - dummy = ListNode() - tail = dummy - - while list1 and list2: - if list1.val < list2.val: - tail.next = list1 - list1 = list1.next - else: - tail.next = list2 - list2 = list2.next - tail = tail.next # Move the tail forward - - if list1: - tail.next = list1 - elif list2: - tail.next = list2 - - return dummy.next - - -def print_list(head: Optional[ListNode]): - current = head - result = [] - while current: - result.append(current.val) - current = current.next - return result - - -# Create list1: 1 -> 2 -> 4 -list1 = ListNode(1) -list1.next = ListNode(2) -list1.next.next = ListNode(4) - -# Create list2: 1 -> 3 -> 4 -list2 = ListNode(1) -list2.next = ListNode(3) -list2.next.next = ListNode(4) - -# Merge the lists -solution = Solution() -merged_list = solution.mergeTwoLists(list1, list2) - -# Print the merged list -print(print_list(merged_list)) - -# Expected output: [1, 1, 2, 3, 4, 4] diff --git a/DSA/Linked-List/node.py b/DSA/Linked-List/node.py deleted file mode 100644 index 484805f..0000000 --- a/DSA/Linked-List/node.py +++ /dev/null @@ -1,60 +0,0 @@ -class Node: - def __init__(self, val) -> None: - self.val = val - self.next = None - - -a = Node("A") -b = Node("B") -c = Node("C") -d = Node("D") - -a.next = b -b.next = c -c.next = d - -# A->B->C->D->null - - -def print_list(head: Node): - result = [] - current = head - while current is not None: - result.append(current.val) - current = current.next - return result - - -# def print_list(head: Node): -# if head is None: -# return [] -# return [head.val] + print_list(head.next) - - -print(print_list(a)) - - -# def sum_linked_list(head): -# sum = 0 -# current = head -# while current is not None: -# sum += int(current.val) -# current = current.next -# return sum - - -def sum_linked_list(head): - if head is None: - return 0 - return head.val + sum_linked_list(head.next) - - -a = Node(1) -b = Node(2) -c = Node(3) -d = Node(4) - -a.next = b -b.next = c -c.next = d -print(sum_linked_list(a)) diff --git a/DSA/Linked-List/remove_duplicate_from_sorted_list.py b/DSA/Linked-List/remove_duplicate_from_sorted_list.py deleted file mode 100644 index 8136090..0000000 --- a/DSA/Linked-List/remove_duplicate_from_sorted_list.py +++ /dev/null @@ -1,21 +0,0 @@ -# 83. Remove Duplicates from Sorted List -# Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. -from typing import Optional - - -# Definition for singly-linked list. -class ListNode: - def __init__(self, val=0, next=None): - self.val = val - self.next = next - - -class Solution: - def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: - current = head - while current: - while current.next and current.next.val == current.val: - current.next = current.next.next - current = current.next - - return head diff --git a/DSA/Linked-List/reverse_linkedlist.py b/DSA/Linked-List/reverse_linkedlist.py new file mode 100644 index 0000000..9416f38 --- /dev/null +++ b/DSA/Linked-List/reverse_linkedlist.py @@ -0,0 +1,37 @@ +class Node: + def __init__(self, val): + self.val = val + self.next = None + + +a = Node("a") +b = Node("b") +c = Node("c") +d = Node("d") + +a.next = b +b.next = c +c.next = d + + +def reverse_list(head): + prev = None + current = head + while current is not None: + next_node = current.next + current.next = prev + prev = current + current = next_node + + return prev + + +def print_linked_list(head): + current = head + while current is not None: + print(current.val) + current = current.next + + +current = reverse_list(a) +print_linked_list(current)