To prove safety offer-- list

Description Title
input a linked list, returns a list according to the order of the tail from the head of 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
        res = []
        while listNode != None:
            res.append(listNode.val)
            listNode = listNode.next
        return res[::-1]

Description Title
to a linked list, wherein if the ring comprises, find the entry ring node list, otherwise, outputs null.

# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def EntryNodeOfLoop(self, pHead):
        # write code here
        dict1 = {}
        while pHead != None:
            if pHead.val not in dict1:
                dict1[pHead.val] = 1
            else:
                return pHead
            pHead = pHead.next

Description Title
in a sorted linked list nodes duplicate, delete the duplicate node list, the node does not retain repeated, returns the head pointer list. For example, the list 1-> 2-> 3-> 3-> 4-> 4-> 5 is treated 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
        if not pHead or not pHead.next:
            return pHead
        head = ListNode(0)
        head.next = pHead
        pre = head
        last = head.next
        while pre and last:
            if last.next and (last.next.val == last.val):
                while last.next and (last.next.val == last.val):
                    last = last.next
                pre.next = last.next
                last = last.next
            else:
                pre = pre.next
                last = last.next
        return head.next
Released nine original articles · won praise 0 · Views 755

Guess you like

Origin blog.csdn.net/weixin_42707571/article/details/105255464