链表 刷题

1.输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列,例如[1,2,3]
    def printListFromTailToHead(self, listNode):
        # write code here
        newlist=[]
        while listNode:
            newlist.insert(0,listNode.val)
            listNode=listNode.next
        return newlist
        '''思路二,利用append
        newList = []
        while listNode:
            newList.append(listNode.val)
            listNode = listNode.next
        return newList[::-1]
        '''

2.输入一个链表,输出该链表中倒数第k个结点。

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def FindKthToTail(self, head, k):
        # write code here
        new_list=[]
        while head:
            new_list.append(head)
            head=head.next
        if k > len(new_list) or k <= 0:
            return None
        return new_list[-k]

3.输入一个链表,反转链表后,输出新链表的表头。

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    # 返回ListNode
    def ReverseList(self, pHead):
        # write code here
        if pHead is None:
            return pHead
        last = None  #指向上一个节点
        while pHead:
            # 先用tmp保存pHead的下一个节点的信息,
            # 保证单链表不会因为失去pHead节点的next而就此断裂
            tmp = pHead.next
            # 保存完next,就可以让pHead的next指向last了
            pHead.next = last
            # 让last,pHead依次向后移动一个节点,继续下一次的指针反转
            last = pHead
            pHead = tmp
        return last

4.输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

# -*- coding:utf-8 -*-
#class ListNode:
#    def __init__(self, x):
#        self.val = x
#        self.next = None
class Solution:
    # 返回合并后列表
    def Merge(self, pHead1, pHead2):
        # write code here
        '''
        #思路一(递归):(1)对空链表存在的情况进行处理,假如 pHead1 为空则返回 pHead2 ,pHead2 为空则返回 pHead1。
        #(2)比较两个链表第一个结点的大小,确定头结点的位置
        #(3)头结点确定后,继续在剩下的结点中选出下一个结点去链接到第二步选出的结点后面,然后在继续重复(2 )(3) 步,直到有链表为空。
        if pHead1 is None:
            return pHead2 
        if pHead2 is None:
            return pHead1
        listMerge = None
        if pHead1.val < pHead2.val:
            listMerge = pHead1
            listMerge.next = self.Merge(pHead1.next,pHead2)
        else:
            listMerge = pHead2
            listMerge.next = self.Merge(pHead2.next,pHead1)
        return listMerge
        
        '''
        #思路二:
        head = ListNode(0)#定义头结点
        first = head 
        while pHead1 != None and pHead2 != None:
            if pHead1.val > pHead2.val:
                head.next = pHead2
                pHead2 = pHead2.next
            else :
                head.next = pHead1
                pHead1 = pHead1.next
            head = head.next
        if pHead1 != None:#若两个链表长度不相等且在链表2元素空了
            head.next = pHead1
        elif pHead2 != None:#若两个链表长度不相等且在链表1元素空了
            head.next = pHead2
        return first.next

5.复杂链表的复制

输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)

# -*- coding:utf-8 -*-
# class RandomListNode:
#     def __init__(self, x):
#         self.label = x
#         self.next = None
#         self.random = None
class Solution:
    # 返回 RandomListNode
    def Clone(self, pHead):
        # write code here
        '''
        #思路一:递归
        if not pHead:
            return None
        while pHead:
            newnode=RandomListNode(pHead.label)
            newnode.random=pHead.random
            newnode.next=self.Clone(pHead.next)
        return newcode
        '''
        #思路二:
        if pHead == None:
            return None
        #(1)复制原来的链表,顺次连接形成新链表
        cloNode = pHead
        while cloNode:
            #第一步核心操作
            node = RandomListNode(cloNode.label)
            node.next = cloNode.next
            cloNode.next = node
            
            cloNode = node.next #下一次操作
        #(2)利用原节点的random指向,来用复制的相应节点的random
        cloNode = pHead
        while cloNode:
            node = cloNode.next #指向复制的结点
            if cloNode.random: #如果原节点有特殊指针
                node.random = cloNode.random.next #则复制的节点的特殊指针指向原节点的特殊指针指向的下一个值  看图更好理解一些
            cloNode = node.next
        #(3)将复制好的链表拆分出来,或者说将 偶数位的节点重新拆分合成新的链表,得到的就是复制的链表
        cloNode = pHead
        pHead = pHead.next
        while cloNode.next:
            #完成第三步的核心操作 此时节点指向隔了一个节点的节点
            node = cloNode.next
            cloNode.next = node.next
            
            cloNode = node #下一个节点的操作
        return pHead

6.两个链表的第一个公共结点

输入两个链表,找出它们的第一个公共结点。

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def FindFirstCommonNode(self, pHead1, pHead2):
        # write code here
        '''
        len1 = self.getChainLen(pHead1)
        len2 = self.getChainLen(pHead2)
        if len2 > len1:
            pHead1, pHead2 = pHead2, pHead1
        diff = abs(len1-len2)
        while diff > 0:
            pHead1 = pHead1.next
            diff -= 1
        while pHead1 != pHead2:
            pHead1 = pHead1.next
            pHead2 = pHead2.next
        return pHead1
    def getChainLen(self, Head):
        chainLen = 0
        while Head:
            chainLen += 1
            Head = Head.next
        return chainLen
        '''
        if pHead1 is None or pHead2 is None:
            return None
        p1,p2 = pHead1,pHead2
        while p1 != p2:
            p1= pHead2 if not p1 else p1.next
            p2= pHead1 if not p2 else p2.next
        return p1
    '''第一种情况:相同长度有交点
两个指针一起走,步长一致,碰到第一个相同的节点 p1 == p1,退出循环,return p1。
第二种情况:相同长度无交点
两个指针一起走,直到走到最后一个节点,p1.next 和 p2.next都为 None,满足 相等的条件,退出循环,return p1。
第三种情况:不同长度有交点
两个指针一起走,当一个指针p1走到终点时,说明p1所在的链表比较短,让p1指向另一个链表的头结点开始走,直到p2走到终点,让p2指向短的链表的头结点,那么,接下来两个指针要走的长度就一样了,变成第一种情况。
第四种情况:不同长度无交点
两个指针一起走,当一个指针p1走到终点时,说明p1所在的链表比较短,让p1指向另一个链表的头结点开始走,直到p2走到终点,让p2指向短的链表的头结点,那么,接下来两个指针要走的长度就一样了,变成第二种情况。
'''

7.链表中环的入口结点

 

给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        #思路一:#遍历链表,环的存在,遍历遇见的第一个重复的即为入口节点
        #if not pHead:
            #return None
        newlist=[]
        p=pHead
        while p:
            if p in newlist:
                return p
            else:
                newlist.append(p)
                p=p.next

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值