注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
一家湾区的startup, CTO是个华人
电面,直接上code- package Signalfx;
- public class FindPosInLinkedList {
- class Node {
- Node next;
-
- }
-
- Node find(Node head, double pos) {
- // 1 / (1 - POS);
- if (head == null || head.next == null) {
- return head;
- }
- if (pos < 0 || pos > 1) {
- return null;
- }
- Node fast = head;
- Node slow = head;
- int count = 1;
- while (fast != null) {
- double num = pos * count;
- if (num >= 1) {
- slow = slow.next;
- num = num - 1;
- }
- fast = fast.next;
- count++;
- }
- return slow;
- }
- }
- /*
- * V [] -> [] -> [] -> [] -> [] ^
- *
- * //pos = 0, return head //pos = 1, return tail //pos = 0.5 return middle //pos
- * = 0.4, 3 node list, return 2nd node //pos = 0.9800033284, 2 node list, return
- * 2nd node //pos = 0.9800033284, 100 node list, return 98th node
- *
- * //null, odd, even, 1000000, pos: -1, 10000, {0.1 0.9899993333, } Node
- * find(Node head, double pos) { // 1 / (1 - POS); if (head == null || head.next
- * == null) { return head; } if (pos < 0 || pos > 1) { return null; } Node fast
- * = head; Node low = head; int count = 1; while (fast != null) { int num = pos
- * * count; if (num >= 1) { slow = slow.next; num = num - 1; } fast = fast.next;
- * count++; }
- *
- * return slow; }
- *
- * 1 / 2 = 0.5 / 1
- *
- * F S
- *
- *
- *
- *
- * F S 1 1 2 1 3 2 4 2 5 3 -
- *
- * //Given the head of a linked list, return the middle of that list //null,
- * odd, even, 1, 2, 10000000 Node findMid(Node head) { if (head == null ||
- * head.next == nulll) { return head; } Node fast = head; Node slow = head;
- * while (fast != null && fast.next != null) { fast = fast.next.next; slow =
- * slow.next; }
- *
- * return slow; }
- */
复制代码 onsite:
016-4-13 04:38):
看了面经,请加分啊~ |