快手java一面
问项目
SQL28 每个供应商成本最低的产品 的变形题,求最高
sql语句和题都没写出来😥,现记录改后的答案:
- sql部分:
SELECT vend_id, MIN(prod_price) AS cheapest_item FROM Products GROUP BY vend_id ORDER BY cheapest_item ASC;
- 重排链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
// 找链表中点
ListNode slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
// 反转后半段链表
ListNode secondList = slow.next;
secondList = reverseList(secondList);
slow.next = null;// 注意此处需要清空,使得head只有前半段,second只有后半段,否则后续连接会陷入死循环
// 依次连接链表(插入节点)
ListNode cur = head;
while (secondList != null && cur != null) {
// 记录分别的next节点防丢失
ListNode firstListNext = cur.next;
ListNode secondListNext = secondList.next;
// 向cur插入secondList
cur.next = secondList;
secondList.next = firstListNext;
// 重置cur和secondList
cur = firstListNext;
secondList = secondListNext;
}
}
public ListNode reverseList(ListNode head) {
if (head == null) return null;
ListNode pre = null, cur = head, con = head.next;
while (con != null) {
cur.next = pre;
pre = cur;
cur = con;
con = cur.next;
}
cur.next = pre;
return cur;
}
} #面试复盘##春招##快手#
