LeetCode开心刷题四十四天——61. Rotate List 82. Remove Duplicates from Sorted List II(时间慢,空间70) 86. Partition List(没想明白一个解法为啥会造成循环)

61. Rotate List
Medium
718 874 Favorite Share

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
1018 85 Favorite Share

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

 

86. Partition List
Medium
803 215 Favorite Share

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

Example:

Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5
 解法最后一步,如果不把head从中间断开最终会导致出现循环,在listnode
这种链表题中经常出现最终处理不当造成循环的问题,这个注释部分是正确解法
如果按照代码中直接出现的会导致循环,但是目前还没搞清原因,尤其是这类题目
非常容易出现循环,应该寻找共性
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 partition(self, head, x):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        small=ListNode(0)
        big=ListNode(0)
        small_root,big_root=small,big
        while head:
            if head.val<x:
                small.next=head
                small=small.next
            else:
                big.next=head
                big=big.next
            # temp=head.next
            # head.next=None
            # head=temp
            head=head.next
        small.next=big_root.next
        return small_root.next

solu=Solution()
head=ListNode(1)
l2=ListNode(4)
l3=ListNode(3)
l4=ListNode(2)
l5=ListNode(5)
l6=ListNode(2)
head.next=l2
l2.next=l3
l3.next=l4
l4.next=l5
l5.next=l6
x=3
ans=solu.partition(head,x)
while True:
    print(ans.val)
    if not ans.next:
        break
    ans=ans.next

 

 

你可能感兴趣的:(LeetCode开心刷题四十四天——61. Rotate List 82. Remove Duplicates from Sorted List II(时间慢,空间70) 86. Partition List(没想明白一个解法为啥会造成循环))