In: Computer Science
In python, how do you move the pointer of a doubly linked list to the last node? Also in python, how do you move the current pointer "n" elements ahead (circularly)
def go_last(self):
# moves 'current' pointer to the last node
def skip(self,n:int):
# moves 'current' pointer n elements ahead (circularly)
Getting to the next node is what we have to do in both the part of the question,
The common part is the node and the doubly linked list class shown below:
class Node: #this class objects are called for node creation
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
class doubly_linked: #to create a doubly linked list
def __init__(self):
self.head = None
# not to get to the last node we have to do the same as tracing and store the previous node till null is the next node
def go_last(self, node):
while (node is not None):
last = node
node = node.next
# when we finish running the above code we'll have the last node stored in the variable last
# now in case of circular list there is no null so we have to change the looping condition for example,
n = int(input(" how many nodes does it has to jump"))
def skip(self,node,n):
while (n>0):
last = node
node = node.next
n = n -1
# after n iterations the loop will end and the last variable will have the nth node
If there is any doubts about the answer let me know, else if it's good, give a thumbs up.
Hope it helped