• 剑指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
  • 相关阅读:
    知识图谱
    spring mvc踩坑记
    学习计划
    迷茫中看见一道曙光
    面试复习准备
    关于配置httpd2.4.18+php5.6
    notepad++ 等用正则表达式自动添加sql引号(宏)
    JDK1.8的HashMap实现原理和源码解析
    Java垃圾回收机制
    sql server 表分区
  • 原文地址:https://www.cnblogs.com/pythonbigdata/p/12734536.html
Copyright © 2020-2023  润新知