Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Exercise on Linked List Basics - Level 1.py #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

decent-uday
Copy link

Here is the Logic to find sum of linked list nodes at odd positions. take an empty list and append the data of nodes in the list. Return the elements in list which are at odd positions.

Here is the Logic to find sum of linked list nodes at odd positions. take an empty list and append the data of nodes in the list.
Return the elements in list which are at odd positions.
@decent-uday
Copy link
Author

lex_auth_012742477906173952821

class Node:
def init(self, data):
self.__data = data
self.__next = None

def get_data(self):
    return self.__data

def set_data(self, data):
    self.__data = data

def get_next(self):
    return self.__next

def set_next(self, next_node):
    self.__next = next_node

class LinkedList:
def init(self):
self.__head = None
self.__tail = None

def get_head(self):
    return self.__head

def get_tail(self):
    return self.__tail

def add(self, data):
    new_node = Node(data)
    if (self.__head is None):
        self.__head = self.__tail = new_node
    else:
        self.__tail.set_next(new_node)
        self.__tail = new_node

def insert(self, data, data_before):
    new_node = Node(data)
    if (data_before == None):
        new_node.set_next(self.__head)
        self.__head = new_node
        if (new_node.get_next() == None):
            self.__tail = new_node

    else:
        node_before = self.find_node(data_before)
        if (node_before is not None):
            new_node.set_next(node_before.get_next())
            node_before.set_next(new_node)
            if (new_node.get_next() is None):
                self.__tail = new_node
        else:
            print(data_before, "is not present in the Linked list")

def display(self):
    temp = self.__head
    while (temp is not None):
        print(temp.get_data())
        temp = temp.get_next()

def find_node(self, data):
    temp = self.__head
    while (temp is not None):
        if (temp.get_data() == data):
            return temp
        temp = temp.get_next()
    return None

def delete(self, data):
    node = self.find_node(data)
    if (node is not None):
        if (node == self.__head):
            if (self.__head == self.__tail):
                self.__tail = None
            self.__head = node.get_next()
        else:
            temp = self.__head
            while (temp is not None):
                if (temp.get_next() == node):
                    temp.set_next(node.get_next())
                    if (node == self.__tail):
                        self.__tail = temp
                    node.set_next(None)
                    break
                temp = temp.get_next()
    else:
        print(data, "is not present in Linked list")

# You can use the below __str__() to print the elements of the DS object while debugging
def __str__(self):
    temp = self.__head
    msg = []
    while (temp is not None):
        msg.append(str(temp.get_data()))
        temp = temp.get_next()
    msg = " ".join(msg)
    msg = "Linkedlist data(Head to Tail): " + msg
    return msg

def find_sum(number_list):
p = []
temp = number_list.get_head()
while (temp is not None):
p.append(temp.get_data())
temp = temp.get_next()
# Write your logic here
return sum(p[i] for i in range(len(p)) if i%2 == 0)

number_list = LinkedList()
number_list.add(10)
number_list.add(20)
number_list.add(30)
number_list.add(40)
number_list.add(50)
number_list.add(60)
number_list.add(70)
number_list.add(80)
number_list.add(90)
number_list.add(100)
number_list.add(110)

print(find_sum(number_list))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant