题目描述1:从尾到头打印链表
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
Python测试:
// An highlighted block
# -*- coding: utf-8 -*-
class ListNode():
# 链表的构造 初始化
def __init__(self, x):
self.val = x
self.next = None
def CreateList(n):
# 链表的创建
if n <= 0:
return False
if n == 1:
return ListNode(1)
else:
listNode = ListNode(1)
tmp = listNode
for i in range(2 , n +1):
tmp.next = ListNode(i)
tmp = tmp.next
return listNode
def PrintList(listNode):
# 用以打印链表结点
tmp = listNode
# 不要改变原来的链表
while tmp:
print(tmp.val)
tmp = tmp.next
def printListFromTailToHead(listNode):
# write code here
l = []
head = listNode
while head:
l.insert(0, head.val)
head = head.next
return l
def main():
listNode = CreateList(11)
# 打印链表进行测试
# PrintList(listNode)
print(printListFromTailToHead(listNode))
if __name__ == "__main__":
main()
https://blog.csdn.net/u010636181/article/details/78236630
题目描述2:链表中环的入口结点
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
Python测试:
// An highlighted block
class Node(object):
def __init__(self,value):
self.val = value
self.next = None
def newfindloop(start):
first = start
second = start
while second:
first = first.next
if second.next == None:
return False
second = second.next.next
if first == second:
return True
return False
#这种思路时间复杂度为O(n),空间复杂度为O(1)
#创建一个链表
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
node5 = Node(5)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node2
print(newfindloop(node1))
https://blog.csdn.net/bai_and_hao_1314/article/details/82223670
// An highlighted block
class Node(object):
def __init__(self,value):
self.val = value
self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
# write code here
if not pHead or not pHead.next or not pHead.next.next:
return None
slow = pHead.next
fast = slow.next
# 找到相遇点
while fast != slow and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
# 慢指针回到表头,快指针留在相遇点,二者同步往前直到相遇在入口结点
slow = pHead
while slow != fast:
fast = fast.next
slow = slow.next
return slow
return None
#创建一个链表
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
node5 = Node(5)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node2
a =Solution()
print(a.EntryNodeOfLoop(node1).val)
题目描述3:删除链表中重复的结点
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
Python测试:
// An highlighted block
class Solution:
def deleteDuplication(self, pHead):
first = ListNode(-1)
first.next = pHead
last = first
while pHead and pHead.next:
if pHead.val == pHead.next.val:
val = pHead.val
while pHead and val==pHead.val:
pHead = pHead.next
last.next = pHead
# print(last.val)
else:
last = pHead
pHead = pHead.next
return first.next
def getNewChart(self, list):
if list:
node = ListNode(list.pop(0))
node.next = self.getNewChart(list)
return node
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
if __name__ == '__main__':
list = [1,1,1,2,3,3,4]
listNode = Solution().getNewChart(list)
head = Solution().deleteDuplication(listNode)
while head:
print(head.val, end=" ")
head = head.next
相关知识点回顾:
链表的基本结构:
链表是通过一个个节点组成的,每个节点都包含了称为cargo的基本单元,它也是一种递归的数据结构。它能保持数据之间的逻辑顺序,但存储空间不必按照顺序存储。
如图:
链表的基本元素有:
节点:每个节点有两个部分,左边部分称为值域,用来存放用户数据;右边部分称为指针域,用来存放指向下一个元素的指针。
head:head节点永远指向第一个节点
tail: tail永远指向最后一个节点
None:链表中最后一个节点的指针域为None值
但链表也分为单向链表和单向循环链表,双向链表和双向循环链表,如图为单向链表和单向循环链表:
https://blog.csdn.net/qq_39422642/article/details/78988976
总结:
从尾到头打印链表:
https://blog.csdn.net/qq_38441207/article/details/88636027
链表中环的入口结点:
https://blog.csdn.net/qq_38441207/article/details/88816543
删除链表中重复的结点:
https://blog.csdn.net/qq_38441207/article/details/88824367