Created
May 1, 2018 21:56
-
-
Save jesuyedavid/06ec8b5e2c703e305c24b69e05c1090c to your computer and use it in GitHub Desktop.
Given k, print the kth element from the end of a linked list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, data): | |
self.data=data | |
self.nex=None | |
def prkte(cur, k): | |
if(cur.nex==None): | |
if(k-1==0): | |
print(cur.data) | |
return k-1 | |
else: | |
num=prkte(cur.nex, k) | |
if num==1: | |
print(cur.data) | |
return(num-1) | |
#create list here | |
lls=Node(1) | |
lls.nex=Node(2) | |
lls.nex.nex=Node(3) | |
lls.nex.nex.nex=Node(4) | |
lls.nex.nex.nex.nex=Node(5) | |
lls.nex.nex.nex.nex.nex=Node(6) | |
lls.nex.nex.nex.nex.nex.nex=Node(7) | |
prkte(lls, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment