python 删除链表的节点

剑指offer第18题:

题目一:在O(1)时间内删除链表节点(给点头节点与删除节点)

题目二:删除连续重复节点

# _*_coding:utf-8 _*_

class ListNode:
    def __init__(self):
        self.value = None
        self.next_node = None

class Solution:
    def list_generate(self, lst):
        """
        生成链表
        """
        if not lst:
            return None
        list_node = ListNode()
        list_node.value = lst[0]
        if len(lst) == 1:
            list_node.next_node = None
        else:
            list_node.next_node = self.list_generate(lst[1:])
        return list_node

    def find_node(self, node, target_value):
        """
        根据给定的目标值,找出指定节点的位置
        非题目要求,只是为了测试验证
        """
        if not target_value:
            return False
        while node:
            if node.value == target_value:
                return node
            node = node.next_node
        return False

    def delete_node(self, head_node, del_node):
        """
        删除指定节点
        """
        if not (head_node and del_node):
            return False

        if del_node.next_node:
            # 删除的节点不是尾节点,而且不是唯一一个节点
            del_next_node = del_node.next_node
            del_node.value = del_next_node.value
            del_node.next_node = del_next_node.next_node
            del_next_node.value = None
            del_next_node.next_node =None

        elif del_node == head_node:
            # 唯一一个节点,删除头节点
            head_node = None
            del_node = None

        else:
            # 删除节点为尾节点
            node = head_node
            while node.next_node != del_node:
                node = node.next_node

            node.next_node = None
            del_node = None

        return head_node


    def is_repeat(self, node):
        """
        题目二:判断是否与后面的节点重复
        """
        if node.next_node:
            if node.value == node.next_node.value:
                return True
        return False

    def delete_repeat_node(self, head_node):
        """
        题目二:删除重复节点
        """
        node = head_node
        flag = False
        """
        flag说明:例如'a'->'a',第一次循环删除第二个'a',然后通过这个flag来进行判断上一次操作是否删除重复节点
        如果是的话,再次判断后面还有没有'a'了,没有,则通过这个flag把第一个'a'也应该删除,
        """
        while node:
            if solution.is_repeat(node):
            # 删除重复节点,并且不进行node=node.next_node,可能出现连续多个重复节点
                head_node = self.delete_node(head_node, node)
                flag = True
            else:
                if flag:
                    head_node = self.delete_node(head_node, node)
                    flag = False
                node = node.next_node
        return head_node


if __name__ == '__main__':
    solution = Solution()
    head_node = solution.list_generate(['a', 'a', 'a', 'b', 'c', 'e', 'd', 'd'])# 测试用例

    # ----------------------------------------------------
    # 题目一: 删除指点节点
    # target_value = 'a'
    # target_node = solution.find_node(head_node, target_value)
    # if target_node:
    #     print target_node.value
    # head_node = solution.delete_node(head_node, target_node)
    # ---------------------------------------------------

    # ---------------------------------------------------
    # 题目二:删除重复节点
    head_node = solution.delete_repeat_node(head_node)
    # --------------------------------------------------

    # 输出,打印新链表
    node = head_node
    if node:
        while node:
            print node.value,
            node = node.next_node
            if node:
                print '->',
    else:
        print 'wrong'


你可能感兴趣的:(python练习)