用于将最后一个元素移动到给定链接列表前面的Python程序
编写一个函数,将给定单链表中的最后一个元素移到前面。例如,如果给定的链表是 1->2->3->4->5,那么函数应该将链表更改为 5->1->2->3->4。
算法:
遍历列表直到最后一个节点。使用两个指针:一个存储最后一个节点的地址,另一个存储倒数第二个节点的地址。循环结束后进行以下操作。
- 使倒数第二个倒数第二个(secLast->next = NULL)。
- 将最后一个的下一个设置为头(last->next = *head_ref)。
- 将最后一个作为头部(*head_ref = last)。
Python3
# Python3 code to move the last item
# to front
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
# Function to add a node
# at the beginning of Linked List
def push(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node
# Function to print nodes in
# a given linked list
def printList(self):
tmp = self.head
while tmp is not None:
print(tmp.data, end = ", ")
tmp = tmp.next
print()
# Function to bring the last node
# to the front
def moveToFront(self):
tmp = self.head
# To maintain the track of
# the second last node
sec_last = None
# To check whether we have not
# received the empty list or list
# with a single node
if not tmp or not tmp.next:
return
# Iterate till the end to get
# the last and second last node
while tmp and tmp.next :
sec_last = tmp
tmp = tmp.next
# Point the next of the second
# last node to None
sec_last.next = None
# Make the last node as the
# first Node
tmp.next = self.head
self.head = tmp
# Driver Code
if __name__ == '__main__':
llist = LinkedList()
# Swap the 2 nodes
llist.push(5)
llist.push(4)
llist.push(3)
llist.push(2)
llist.push(1)
print (
"Linked List before moving last to front ")
llist.printList()
llist.moveToFront()
print (
"Linked List after moving last to front ")
llist.printList()
输出:
Linked list before moving last to front
1 2 3 4 5
Linked list after removing last to front
5 1 2 3 4
时间复杂度: O(n),其中 n 是给定链表中的节点数。
有关详细信息,请参阅有关将最后一个元素移动到给定链接列表前面的完整文章!