LeetCode:链表(2)

LeetCode:链表(2)

文章目录

  • LeetCode:链表(2)
    • 237.删除链表中的节点
    • 203.移除链表元素
    • 160. 相交链表
    • 234. 回文链表

237.删除链表中的节点

LeetCode:链表(2)_第1张图片
刚开始看没搞懂啥意思。。。。。。要删除的节点已经给你找出来了,只需要将其覆盖就可以

class Solution:
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        node.val = node.next.val
        node.next = node.next.next

206.反转链表
LeetCode:链表(2)_第2张图片
思路1:三个指针

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        pre,cur = None,head
        while cur:
            tmp = cur.next
            cur.next = pre
            pre = cur
            cur = tmp
        return pre

203.移除链表元素

LeetCode:链表(2)_第3张图片

思路:前后指针

class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        
        while head and head.val==val:
            head = head.next
        
        if not head: return head
        pre,cur = head,head.next
        while cur:
            if cur.val == val:
                pre.next=cur.next
            else:
                pre = cur
            cur = cur.next
        return head

160. 相交链表

LeetCode:链表(2)_第4张图片
LeetCode:链表(2)_第5张图片

思路1:方法可行,但是超出时间限制。。。

#第一思路暴力法
        cur1,cur2 = headA,headB
        while cur1:
            while cur2:
                if cur2 is cur1:
                    return cur2
                cur2 = cur2.next
            cur2 = headB
            cur1 = cur1.next
        return cur1

思考:如果出现两者永不相交的情况的话,就会出现死循环。
改进:

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        #第一思路暴力法
        cur1,cur2 = headA,headB
        while cur1 is not cur2:
            cur1 = cur1.next if cur1 else headB
            cur2 = cur2.next if cur2 else headA
        return cur1

思路解读:为什么会出现相互赋值初始头结点的情况?

短链表走到尽头后继续走长链表,就相当于短链表+长链表。长链表走到尽头后又走短链表,相当于长链表+短链表。所以重新走两次其实本质上还是相当于走两次新的拼接的互补链表,一定会找到交点。
如果出现不相交的情况:
因为headA+headB和headB+ headA长度一定是相等的。所以最后一定会出现同时为None的情况来确保不会出现死循环。

方法2:字典模拟哈希表
将其中一个链表存储到字典中,另一个遍历,看是否存在于字典,若存在,则说明有共同节点;若不存在,则说明没有共同节点。

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        #字典模拟哈希表
        dic = {
     }
        cur1,cur2 = headA,headB
        while cur1:
            dic[cur1] = 1
            cur1 = cur1.next
        while cur2:
            if cur2 in dic:
                return cur2
            cur2 = cur2.next
        return cur2

234. 回文链表

LeetCode:链表(2)_第6张图片

方法1:将值读出存储为字符串

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        str1 = ''
        cur = head
        while cur:
            str1 += (str(cur.val))
            cur = cur.next
        num = len(str1)
        return str1 == str1[::-1]

剑指offer22.链表中倒数第k个节点
LeetCode:链表(2)_第7张图片

方法1:

class Solution:
    def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
        count = 1-k
        cur = head
        while cur:
            cur = cur.next
            count +=1

        cur = head
        while count:
            if count == 1: return cur
            count -= 1
            cur = cur.next

你可能感兴趣的:(LeetCode:链表(2))