python - Incorrectly returning the value of a Node at position 'm' in a linked list -
i have homework problem:
find element in singly linked list that's m elements end. example, if linked list has 5 elements, 3rd element end 3rd element. function definition should question5(ll, m), ll first node of linked list , m "mth number end". should copy/paste node class below use representation of node in linked list. return value of node @ position.
the code paste 4-line definition of class node. attempt follows.
class node(object): def __init__(self, data): self.data = data self.next = none ll = none # provided node class. class node(object): def __init__(self, data): self.data = data self.next = none # function adding values list. def add(new_data): global ll node = node(new_data) node.next = ll ll = node def question5(ll, m): # start of list. element = ll # node count. c = 0 # set end of list false. endlist = false # loop until linked list reaches end. while not endlist: c += 1 element = element.next if element.next == none: endlist = true # set node value count - position m position = c - m # if position less 0, end has been reached if(m < 0): return "8, end of list" # if not end of list return value @ position m else: return position global ll add(1) add(2) add(3) add(4) add(5) add(6) add(7) add(8) print ("value of node @ position m end is: ", question5(ll, -1)) #('value of node @ position m end is: ', '8, end of list') print ("value of node @ position m end is: ", question5(ll, 3)) #('value of node @ position m end is: ', 4) print ("value of node @ position m end is: ", question5(ll, 2)) #('value of node @ position m end is: ', 5) print ("value of node @ position m end is: ", question5(ll, 0)) #('value of node @ position m end is: ', 7)
my reviewer says solution incorrect, not returning actual value of node @ position 'm'. correct way return value of node object in scenario?
edit: sake of finalizing question have included solution able implement per advice in second answer below
new code:
ll = none # provided node class. class node(object): def __init__(self, data): self.data = data self.next = none # function adding values list. def add(new_data): global ll node = node(new_data) node.next = ll ll = node def question5(ll, m): # nodes traversed element1 = ll # nodes beginning element2 = ll # node count. c = 0 # traverse list until c = m if(ll not none): while(c < m): element2 = element2.next c += 1 # loop until linked list reaches end. while(element2 not none): #print ("this element 1:", element1.data) #print ("this element 2:", element2.data) element1 = element1.next element2 = element2.next # return current node @ m steps end return element1.data global ll # list meant represent telephone 0 being end. add("0") add("9 wxyz") add("8 tuv") add("7 pqrs") add("6 mno") add("5 jkl") add("4 ghi") add("3 def") add("2 abc") add("1") # singly linked list: # 1 - 2 abc - 3 def - 4 ghi - 5 jkl - 6 mno - 7 pqrs - 8 tuv - 9 wxyz - 0 print ("node @ position m in relation end of ll is: ", question5(ll, 4)) # ('node @ position m in relation end of ll is: ', '7 pqrs') print ("node @ position m in relation end of ll is: ", question5(ll, 8)) # ('node @ position m in relation end of ll is: ', '3 def') print ("node @ position m in relation end of ll is: ", question5(ll, 1)) # ('node @ position m in relation end of ll is: ', '0') print """---end question 5---
look @ output: when m 2, code returns 5. problem statement shows expects 7, second element counting form end of list. code backs off 2 elements: apparently, way allow returning 8 give position of -1, , 0 required value of 7.
fix counting, , you'll fine; you're handling traversal correctly.
now i've had time on code, i'm going off previous answer somewhat: end of list properly, that's code correctly. @ functional standpoint, reviewer entirely correct: don't return node's value @ all. compute position , assume node's value matches. you're not allowed that.
consider list built way, on phone buttons:
add("abc") add("def") add("ghi") add("jkl") add("mno") add("prs") add("tuv") add("wxy") add("qz")
if input 2, have return "wxy". tried question5 code, , still returns numbers. wrong.
you have find way end of list, , find proper value end of list. there various ways this. instance, build list of node values traverse list; print value position -m in list. possibility use recursion find end of list, return counter recursion chain figure out instance should print value of current node.
can take next steps on own.
Comments
Post a Comment