这是个不能称作问题集合的问题集合
LEETCODE25: K个一组反转链表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
class Solution: def reverse(self,head,tail): prev=tail.next cur =head while cur!=prev.next: next_p=cur.next cur.next=prev prev=cur cur=next_p return tail,head def reverseKGroup(self, head: ListNode, k: int) -> ListNode: dummy = ListNode(0) dummy.next = head prev=dummy cur=head while cur: tail=prev for i in range(k): tail=tail.next if not tail: return dummy.next next_p =tail.next cur,tail =self.reverse(cur,tail) prev.next=cur tail.next=next_p prev=tail cur=next_p return dummy.next
|
这次写的倒是还行 。就是反转这边出了一点问题。肌肉记忆了都快
LEETCODE23.合并K个升序链表
不会写
这个归并妙啊 三目表达式用的好。这边为什么要那啥呢
归并还是很漂亮的~
思路还是清晰的~
okok股票完就下班
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
class Solution { public: ListNode* mergeTwoList(ListNode *l1,ListNode* l2){ if (l1 == nullptr)return l2; if (l2 == nullptr)return l1; ListNode *cur = new ListNode; ListNode *dummy = new ListNode; cur =dummy; while (l1 && l2){ if (l1->val<l2->val){ dummy->next=l1; l1=l1->next; }else{ dummy->next=l2; l2=l2->next; } dummy=dummy->next; } if (l1 != nullptr)dummy->next=l1; if (l2 != nullptr)dummy->next=l2; return cur->next; } ListNode* merge(vector<ListNode*>& lists,int left,int right){ if(left==right){ return lists[left]; } if(left>right){ return nullptr; } int mid = (left+right)>>1; return mergeTwoList(merge(lists,left,mid),merge(lists,mid+1,right));
} ListNode* mergeKLists(vector<ListNode*>& lists) { return merge(lists,0,lists.size()-1); } };
|