patternpythonMinor
Finding the kth to last element of a singly linked list
Viewed 0 times
lastthefindingsinglyelementkthlistlinked
Problem
I'm not sure if this code is industry-standard or not. Please review it and let me know if any improvement is required.
class Node(object):
def __init__(self,data,next):
self.data=data
self.next=next
class getLinkRest():
head = None
tail= None
def insert(self,data):
node=Node(data,None)
if self.head is None:
self.head=self.tail=node
else:
self.tail.next=node
self.tail=node
def showFromItem(self,data):
curr_node=self.head
flag=None
while curr_node is not None:
if curr_node.data == data or curr_node.data > data:
print curr_node.data
flag =1
curr_node=curr_node.next
if(flag is None):
print "Not exist"
s= getLinkRest()
s.insert(1)
s.insert(2)
s.insert(3)
s.insert(4)
s.insert(5)
s.showFromItem(3)
s.showFromItem(7)Solution
Fix the indentation
Before I say anything, let's reformat your code,
for everyone's sake,
because as you posted it, it just doesn't work,
because indentation is crucial in Python.
Please be more careful next time.
Purpose of the code
Finding the kth to last element of a singly linked list
This is not what the posted code does.
The
and prints that element and the rest of the elements until the end of the linked list that are bigger than the the specified value, or else it prints the text "Not exist".
For example:
The above code will print 13 and 15.
That's a very unusual logic, and somehow I doubt that this was really your intention.
It's good to have a clear purpose, and to include a clear problem statement in a question.
Unused constructor argument
The
So you could simplify it:
Instead of using
it would be more natural to make it a boolean, like this:
You can write this condition shorter:
Like this:
Before I say anything, let's reformat your code,
for everyone's sake,
because as you posted it, it just doesn't work,
because indentation is crucial in Python.
Please be more careful next time.
class Node(object):
def __init__(self, data, next):
self.data = data
self.next = next
class getLinkRest():
head = None
tail = None
def insert(self, data):
node = Node(data, None)
if self.head is None:
self.head = self.tail = node
else:
self.tail.next = node
self.tail = node
def showFromItem(self, data):
curr_node = self.head
flag = None
while curr_node is not None:
if curr_node.data == data or curr_node.data > data:
print curr_node.data
flag = 1
curr_node = curr_node.next
if flag is None:
print "Not exist"
s = getLinkRest()
s.insert(1)
s.insert(2)
s.insert(3)
s.insert(4)
s.insert(5)
s.showFromItem(3)
s.showFromItem(7)Purpose of the code
Finding the kth to last element of a singly linked list
This is not what the posted code does.
The
showFromItem function searches for an element with the specified value,and prints that element and the rest of the elements until the end of the linked list that are bigger than the the specified value, or else it prints the text "Not exist".
For example:
s.insert(11)
s.insert(12)
s.insert(13)
s.insert(4)
s.insert(15)
s.showFromItem(13)The above code will print 13 and 15.
That's a very unusual logic, and somehow I doubt that this was really your intention.
It's good to have a clear purpose, and to include a clear problem statement in a question.
Unused constructor argument
The
next value of the constructor is never used.So you could simplify it:
class Node(object):
def __init__(self, data):
self.data = data
self.next = NoneInstead of using
flag with None and 1 as values,it would be more natural to make it a boolean, like this:
found = False
while curr_node is not None:
if curr_node.data == data or curr_node.data > data:
print(curr_node.data)
found = True
curr_node = curr_node.next
if not found:
print("Not exist")You can write this condition shorter:
if curr_node.data == data or curr_node.data > data:Like this:
if curr_node.data >= data:Code Snippets
class Node(object):
def __init__(self, data, next):
self.data = data
self.next = next
class getLinkRest():
head = None
tail = None
def insert(self, data):
node = Node(data, None)
if self.head is None:
self.head = self.tail = node
else:
self.tail.next = node
self.tail = node
def showFromItem(self, data):
curr_node = self.head
flag = None
while curr_node is not None:
if curr_node.data == data or curr_node.data > data:
print curr_node.data
flag = 1
curr_node = curr_node.next
if flag is None:
print "Not exist"
s = getLinkRest()
s.insert(1)
s.insert(2)
s.insert(3)
s.insert(4)
s.insert(5)
s.showFromItem(3)
s.showFromItem(7)s.insert(11)
s.insert(12)
s.insert(13)
s.insert(4)
s.insert(15)
s.showFromItem(13)class Node(object):
def __init__(self, data):
self.data = data
self.next = Nonefound = False
while curr_node is not None:
if curr_node.data == data or curr_node.data > data:
print(curr_node.data)
found = True
curr_node = curr_node.next
if not found:
print("Not exist")if curr_node.data == data or curr_node.data > data:Context
StackExchange Code Review Q#98156, answer score: 7
Revisions (0)
No revisions yet.