zoukankan      html  css  js  c++  java
  • 剑指offer分块总结----------链表

    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
            array=[]
            if not listNode:
                return []
            while listNode:
                array.append(listNode.val)
                listNode=listNode.next
            return array[::-1]

    2、链表中环的入口结点

    题目描述

    给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
    class Solution:
        def EntryNodeOfLoop(self, pHead):
            # write code here
            if not pHead and not pHead.next:
                return None
            first=pHead
            second=pHead
            while second and second.next:
                first=first.next
                second=second.next.next
                if first==second:
                    first=pHead
                    while first!=second:
                        first=first.next
                        second=second.next
                    return first
            return None

    3、删除链表中重复的结点

    题目描述

    在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
    例如,链表1->2->3->3->4->4->5 处理后为 1->2->5
    # -*- coding:utf-8 -*-
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution:
        def deleteDuplication(self, pHead):
            # write code here
            new_l=ListNode(-1)
            cur=pHead
            new_l.next=pHead
            d=new_l
            while cur:
                if cur.next and cur.val==cur.next.val:
                    temp=cur.next
                    while temp and temp.val==cur.val:
                        temp=temp.next
                    d.next=temp
                    cur=temp
                else:
                    d=cur
                    cur=d.next
            return new_l.next
  • 相关阅读:
    ArcGIS为面要素生成邻接矩阵
    图片整理备份
    导出CityGML
    [ML] 数据处理
    微信公众号开发之access_token的全局共用
    DataReader转Dictionary数据类型之妙用
    标准化接口系统改造
    利用通用权限管理系统底层解决数据从不同库的导入导出问题
    通用权限管理系统中数据权限功能开发及使用说明
    常用API接口签名验证参考
  • 原文地址:https://www.cnblogs.com/pythonbigdata/p/12734536.html
Copyright © 2011-2022 走看看