[LinkedList]025 Reverse Nodes in k-Group

  • 分類:LinkedList

  • 考察知識點(diǎn):LinkedList 遞歸 Reverse

  • 最優(yōu)解時間復(fù)雜度:**O(n) **

25. Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

Example:

Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

Note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list's nodes, only nodes itself may be changed.

代碼:

我的方法(超時了):

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseKGroup(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        node_list=[]
        dummy=ListNode(0)
        p=head
        end=head
        p1=dummy
        count=0
        while(p!=None):
            node_list.append(p)
            p=p.next
            count+=1
            if(count==k):
                while(len(node_list)!=0):
                    p1.next=node_list.pop()
                    end=end.next
                    p1=p1.next
                count=0
                
        while(end!=None):
            p1.next=end
            end=end.next
            p1=p1.next
            
        return dummy.next

最佳方法:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def reverseKGroup(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        #確定邊界
        if head==None or head.next==None:
            return head
        
        #走一走
        count=0
        cur=head
        while(cur!=None and count!=k):
            cur=cur.next
            count+=1
        
        #reverse
        if count==k:
            cur=self.reverseKGroup(cur, k)
            while(count):
                count-=1
                temp=head.next
                head.next=cur
                cur=head
                head=temp
            head=cur
            
        return head

討論:

1.大佬的方法超級快,基本上也是看懂了,但是感覺自己在reverse上還是十分的笨拙- -可能對reverse的題型這種還是要多加練習(xí)
2.這道題目可能不咋會考,emm所以似乎也無所謂了,但是還是要掌握reverse!

大佬方法,跪拜大佬
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,872評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,223評論 0 23
  • 從初夏到深秋, 柳絮飛過,銀杏飄落, 紫薇盛開的不是特么熱烈, 牛不吃的牛繁縷, 看著。 毛栗子殼薄肉厚, 碧根果...
    阿琴的精彩閱讀 348評論 0 1
  • 文/彬睿 今天,早早的吃完早餐,我們?nèi)規(guī)厦妹镁团d高采烈的出門了,因?yàn)槲乙桶职謰寢屢黄鹑Q戰(zhàn)“峨眉山之巔”。坐...
    茉莉初綻閱讀 293評論 0 2
  • 學(xué)無止境,人要不斷的學(xué)習(xí),不斷的成長,好女孩已經(jīng)成了我的家,走進(jìn)好女孩兩個月了,從開始的陌生到熟悉,再從熟悉到家一...
    格拉多披薩餐廳wen閱讀 1,108評論 0 2

友情鏈接更多精彩內(nèi)容