class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def printListFromTailToHead(self , listNode: ListNode) -> List[int]:
#用栈记录遍历的结果,然后返回出栈结果
if listNode is None:
return []
stack = []
p = listNode
while p:
stack.append(p.val)
p = p.next
return stack[::-1]
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def ReverseList(self , head: ListNode) -> ListNode:
#使用递归
#链表第一步,判断链表是否为空
if head is None or head.next is None:
return head
#想不通就画一个链表只有两个节点,然后连接
last = self.ReverseList(head.next)
head.next.next = head
head.next = None
return last
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def Merge(self , pHead1: ListNode, pHead2: ListNode) -> ListNode:
#新设置一个链表,分别用两个指针指向原来的链表,对比大小,将小的节点接到新链表后
#判断两个链表是否为空
if pHead1 is None:
return pHead2
if pHead2 is None:
return pHead1
#设置新链表的头结点
dummp = ListNode(-1)
p = dummp
p1 = pHead1
p2 = pHead2
#两个链表的指针都必须存在
while p1 and p2:
#比较大小
if p1.val > p2.val:
p.next = p2
p2 = p2.next
else:
p.next = p1
p1 = p1.next
p = p.next
#出循环后,可能有一个指针不是空
if p1:
p.next = p1
if p2:
p.next = p2
return dummp.next
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def FindFirstCommonNode(self , pHead1 , pHead2 ):
#分别找两个指针指向两个链表,如果指针1为空,就指向链表2,指针2为空就指向链表1,当两个指针相同时(可能都是空或者相遇了)跳出循环。(两个指针都走了链表1+链表2的长度)
#判断链表是否为空
if pHead1 is None:
return pHead1
if pHead2 is None:
return pHead2
p1 = pHead1
p2 = pHead2
while p1 != p2:
if p1 is None:
p1 = pHead2
else:
p1 = p1.next
if p2 is None:
p2 = pHead1
else:
p2 = p2.next
return p1
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
#使用快慢指针,先让快慢指针相遇,确定有环,然后将快指针指回头结点,和慢指针一起重新走一遍,步长为1,相遇节点就是环入口。
#判断链表是否为空
if pHead is None:
return None
fast = pHead
slow = pHead
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
break
if fast is None or fast.next is None: #没有环而退出了循环
return None
else:
fast = pHead
while fast != slow:
fast = fast.next
slow = slow.next
return fast
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def FindKthToTail(self , pHead: ListNode, k: int) :
#使用快慢指针,让快指针先走k步
if pHead is None:
return pHead
fast = pHead
slow = pHead
for _ in range(k): #走k步
if not fast: #如果中间出现了空,就返回None,否则继续走
return None
fast = fast.next
else:
while fast:
slow = slow.next
fast = fast.next
return slow
class RandomListNode:
def __init__(self, x):
self.label = x
self.next = None
self.random = None
class Solution:
def Clone(self, pHead):
#使用hash表来存储新链表与旧链表的节点,key:旧链表节点,value:新链表节点
#两次循环,第一次遍历旧链表,复制next节点,同时保存hash。第二次遍历hash,给新链表指定random指针
#先判断链表是否为空
if pHead is None:
return pHead
#设置新链表的头结点
dummp = RandomListNode(-1)
p = dummp
cur = pHead
hash_dict = dict()
while cur:
clone = RandomListNode(cur.label) #创建新节点
p.next = clone #新链表指向新节点
hash_dict[cur] = clone #hash表保存旧节点和新节点
cur = cur.next #移动旧链表指针
p = p.next #移动新链表指针
for (key,value) in hash_dict.items(): #遍历hash表
if key.random is None:
value.random = None
else:
value.random = hash_dict[key.random] #key.radom是旧链表中的值,在hash表中可以拿到新链表的对应值。
return dummp.next
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def deleteDuplication(self , pHead: ListNode):
#给链表加头结点,方便删除第一个元素,两两比对链表,如果相等,就循环删除
#判断链表是否为空
if pHead is None or pHead.next is None:
return pHead
#设置头结点
dummp = ListNode(-1)
p = dummp
p.next = pHead
while p.next and p.next.next: #两两比较
if p.next.val == p.next.next.val:
temp = p.next.val #先保存当前等值
while p.next and p.next.val == temp: #循环删除p.next
p.next = p.next.next
else:
p = p.next #移动p指针
return dummp.next
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def deleteNode(self , head: ListNode, val: int) -> ListNode:
#先判断表是否为空
if head is None:
return head
#设立头结点,可能要删第一个节点
dummp = ListNode(-1)
p = dummp
p.next = head
while p.next:
if p.next.val == val: #删除p.next
p.next = p.next.next
else:
p = p.next
return dummp.next