Leetcode 链表

做题记录

203. 移除链表元素

虚拟头节点

class Solution(object):
    def removeElements(self, head, val):
        dummy=ListNode(next=head)
        cur=dummy
        while cur.next!=None:
            if cur.next.val!=val:
                cur=cur.next
            else:
                cur.next=cur.next.next
        return dummy.next

707. 设计链表

class Node(object):
    def __init__(self,val):
        self.val=val
        self.next=None
class MyLinkedList(object):
    def __init__(self):
        self.dummy=Node(0)
        self.count=0
    def get(self, index):
        if 0<=index<self.count:
            cur=self.dummy
            for _ in range(index+1):
                cur=cur.next
            return cur.val
        else:return -1
    def addAtHead(self, val):
        self.addAtIndex(0,val)
    def addAtTail(self, val):
        self.addAtIndex(self.count,val)
    def addAtIndex(self, index, val):
        if index<0:index=0
        elif index>self.count:return
        self.count+=1
        add=Node(val)
        prev,cur=None,self.dummy
        for _ in range(index+1):
            prev,cur=cur,cur.next
        else:prev.next,add.next=add,cur
    def deleteAtIndex(self, index):
        if 0<=index<self.count:
            self.count-=1
            prev,cur=None,self.dummy
            for _ in range(index+1):
                prev,cur=cur,cur.next
            else:
                prev.next,cur.next=cur.next,None

206. 反转链表

tmp存储cur.next

class Solution(object):
    def reverseList(self, head):
        prev,cur=None,head
        while cur!=None:
            tmp=cur.next
            cur.next=prev
            prev,cur=cur,tmp
        return prev

24. 两两交换链表中的节点

class Solution(object):
    def swapPairs(self, head):
        dummy=ListNode(0,head)
        cur=dummy
        while cur.next!=None and cur.next.next!=None:
            tmp=cur.next
            tmp1=cur.next.next.next
            cur.next=cur.next.next
            cur.next.next=tmp
            cur.next.next.next=tmp1
            cur=cur.next.next
        return dummy.next

19. 删除链表的倒数第 N 个结点

fast指针先走n步,fast和slow再一起走,当fast走到末尾节点,slow节点在倒数第n个节点的前一个节点

class Solution(object):
    def removeNthFromEnd(self, head, n):
        dummy=ListNode(0,head)
        l,r=dummy,dummy

        while n!=0:
            r=r.next
            n-=1
        while r.next!=None:
            r=r.next
            l=l.next
        l.next=l.next.next
        return dummy.next

142. 环形链表 II

判断链表是否有环:fast指针每次走两步,slow指针每次走一步,若能相遇则有环。

若有环,环入口为:一个指针指向头节点,一个指针指向相遇节点,每次一步,相遇处则为环入口

class Solution(object):
    def detectCycle(self, head):
        fast,slow=head,head
        while fast!=None and fast.next!=None:
            fast=fast.next.next
            slow=slow.next
            if fast==slow:
                tmp1=head
                tmp2=fast
                while tmp1!=tmp2:
                    tmp1=tmp1.next
                    tmp2=tmp2.next
                return tmp1
        return None

 141. 环形链表

class Solution(object):
    def hasCycle(self, head):
        dummy=ListNode(0,head)
        slow,fast=dummy,dummy
        while fast!=None and fast.next!=None:
            slow=slow.next
            fast=fast.next.next
            if slow==fast:return True
        return False

面试题 02.07. 链表相交

求AB之间链表长度差,让长链表指针指在短链表长度位置,同时移动找相同节点

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        cura,curb=headA,headB
        lena,lenb=0,0
#求长度
        while cura!=None:
            cura=cura.next
            lena+=1
        while curb!=None:
            curb=curb.next
            lenb+=1
        cura,curb=headA,headB
##让cura指向最长链表
        if lena<lenb:
            lena,lenb=lenb,lena
            cura,curb=curb,cura
        step=lena-lenb
        while step!=0:
            cura=cura.next
            step-=1
        while cura!=None:
            if cura==curb:return cura
            else:
                cura=cura.next
                curb=curb.next
        return None

234. 回文链表

数组模拟

class Solution(object):
    def isPalindrome(self, head):
        res=[]
        cur=head
        while cur!=None:
            res.append(cur.val)
            cur=cur.next
        i,j=0,len(res)-1
        while i<=j:
            if res[i]==res[j]:
                i+=1
                j-=1
            else:return False
        return True

fast走两步,slow走一步,当fast走到末尾时slow停在中间,以slow为界划分前后两个链表,将slow后链表倒转,再与原链表比较

class Solution(object):
    def isPalindrome(self, head):
        fast,slow=head,head
        while fast!=None and fast.next!=None:
            slow=slow.next
            fast=fast.next.next
        node=None
        while slow:
            slow.next,slow,node=node,slow.next,slow
        cur=head
        while node:
            if node.val==cur.val:
                node=node.next
                cur=cur.next
            else:return False
        return True

143. 重排链表

将头节点之后的节点全部存进队列

class Solution(object):
    def reorderList(self, head):
        from collections import deque
        d=deque()
        cur=head
        while cur.next!=None:
            d.append(cur.next)
            cur=cur.next
        cur=head
        while len(d):
            cur.next=d.pop()
            cur=cur.next
            if len(d):
                cur.next=d.popleft()
                cur=cur.next
        cur.next=None
        return head

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值