LeetCode开心刷题四十四天——61. Rotate List 82. Remove Duplicates from Sorted List II(时间慢,空间70)

时间:2019-09-19
本文章向大家介绍LeetCode开心刷题四十四天——61. Rotate List 82. Remove Duplicates from Sorted List II(时间慢,空间70),主要包括LeetCode开心刷题四十四天——61. Rotate List 82. Remove Duplicates from Sorted List II(时间慢,空间70)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
61. Rotate List
Medium

Given a linked list, rotate the list to the right by k places, where k is non-negative.

Example 1:

Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:

Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL
很特殊的一点是采用先构成环,改变开头再断开的方式
# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if k==0:return head
        if head==None:return head
        dummy=ListNode(0)
        dummy.next=head
        p=dummy
        count=0
        while p.next:
            p=p.next
            count+=1
        # 这里成环了,p和dummy是相等的,dummy代表的是开始的数值,p已经走到了结尾,结尾又连到了开始
        p.next=dummy.next
        step=count-(k%count)
        for i in range(0,step):
            p=p.next

        head=p.next
        # 在这步之前都是成环的,输出head会不停的,之前形成环,算step在合适的地方剪断
        p.next=None
        # while True:
        #     print("start")
        #     print(head.val)
        #     if not head.next: break
        #     head = head.next
        return head



solu=Solution()
head=ListNode(1)
l2=ListNode(2)
l3=ListNode(3)
l4=ListNode(4)
l5=ListNode(5)
head.next=l2
l2.next=l3
l3.next=l4
l4.next=l5
k=2
ans=solu.rotateRight(head,k)
while True:
    print(ans.val)
    if not ans.next:break
    ans=ans.next
82. Remove Duplicates from Sorted List II
Medium

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

Example 1:

Input: 1->2->3->3->4->4->5
Output: 1->2->5

Example 2:

Input: 1->1->1->2->3
Output: 2->3
import collections
# from collections import Counter
# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        root=ListNode(0)
        root.next=head
        val_list=[]
        # 形成字典
        while head:
            val_list.append(head.val)
            head=head.next
        counter=collections.Counter(val_list)
        head=root
        while head.next:
            if counter[head.next.val]!=1:
                head.next=head.next.next
            else:
                head=head.next
        return root.next

solu=Solution()
head=ListNode(1)
l2=ListNode(1)
l3=ListNode(2)
l4=ListNode(2)
l5=ListNode(4)
head.next=l2
l2.next=l3
l3.next=l4
l4.next=l5
ans=solu.deleteDuplicates(head)
while True:
    print(ans.val)
    if not ans.next:
        break
    ans=ans.next
 

原文地址:https://www.cnblogs.com/Marigolci/p/11547209.html