LeetCode探索模块初级算法链表章节python3代码实现

删除链表中的节点

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

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

删除链表的倒数第N个节点

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        if head.next is None:
            head = None
            return

        pre = head
        cur = head
        count = 1
        while cur.next != None:
            cur = cur.next
            count += 1
            if count > n+1:
                pre = pre.next
        if count == n:
            head = head.next
        else:
            pre.next = pre.next.next
        return head


反转链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head == None or head.next == None:
            return head

        stern = head.next
        new_head = self.reverseList(stern)
        head.next = None
        stern.next = head
        return new_head

合并两个有序链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def mergeTwoLists(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        cur1 = l1
        cur2 = l2
        head = ListNode(0)
        cur3 = head
        while cur1 != None and cur2 != None:
            if cur1.val > cur2.val:
                cur3.next = cur2
                cur2 = cur2.next
            else:
                cur3.next = cur1
                cur1 = cur1.next
            cur3 = cur3.next
        if cur1 is None:
            cur3.next = cur2
        else:
            cur3.next = cur1
        return head.next

回文链表

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head == None or head.next == None:
            return head

        stern = head.next
        new_head = self.reverseList(stern)
        head.next = None
        stern.next = head
        return new_head

    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if head is None or head.next is None:
            return True
        cur_s = head#慢指针
        cur_f = head#快指针
        while cur_f.next != None and cur_f.next.next != None:
            cur_f = cur_f.next.next
            cur_s = cur_s.next
        #当节点个数为奇数,此时慢指针在中间
        #当节点个数为偶数,此时慢指针在中间靠近头的一侧
        #接下来将慢指针所指的下一个节点以及后面的节点翻转
        cur_s_next = cur_s.next
        cur_s.next = self.reverseList(cur_s_next)
        cur_s = cur_s.next
        cur = head
        while cur_s != None:
            if cur.val != cur_s.val:
                return False
            else:
                cur = cur.next
                cur_s = cur_s.next
        return True

环形链表

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def hasCycle(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        if head is None:
            return False
        cur_s = head#慢指针
        cur_f = head.next#快指针
        while cur_s != cur_f and cur_f != None and cur_f.next != None:
            cur_s = cur_s.next
            cur_f = cur_f.next.next
        if cur_s == cur_f:
            return True
        else:
            return False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值