https://leetcode-cn.com/problems/intersection-of-two-linked-lists/

    1. /**
    2. * Definition for singly-linked list.
    3. * class ListNode {
    4. * val: number
    5. * next: ListNode | null
    6. * constructor(val?: number, next?: ListNode | null) {
    7. * this.val = (val===undefined ? 0 : val)
    8. * this.next = (next===undefined ? null : next)
    9. * }
    10. * }
    11. */
    12. // a:1->2->3->4->5->6->null->9->5->6->null
    13. // b:9->5->6->null->1->2->3->4->5->6->null
    14. function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
    15. if (!headA || !headB) return null;
    16. let a = headA, b = headB;
    17. while (a !== b) {
    18. a = a ? a.next : headB;
    19. b = b ? b.next : headA
    20. }
    21. return a;
    22. };
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>拖拽的DIV</title>
    8. </head>
    9. <body>
    10. <div id="box"></div>
    11. </body>
    12. </html>
    13. <style>
    14. * {
    15. margin: 0;
    16. padding: 0;
    17. }
    18. #box {
    19. position: absolute;
    20. left: 50%;
    21. top: 50%;
    22. width: 60px;
    23. height: 60px;
    24. border-radius: 50%;
    25. background: blueviolet;
    26. cursor: move;
    27. }
    28. </style>
    29. <script>
    30. const box = document.getElementById('box');
    31. let offsetX, offsetY;
    32. box.onmousedown = function (e) {
    33. const { left, top } = box.getBoundingClientRect();
    34. offsetX = e.clientX - left;
    35. offsetY = e.clientY - top;
    36. document.onmousemove = handleMove;
    37. document.onmouseup = handleStop;
    38. }
    39. function handleMove(e) {
    40. let left = e.clientX - offsetX;
    41. let top = e.clientY - offsetY;
    42. const width = document.documentElement.clientWidth - box.offsetWidth;
    43. const height = document.documentElement.clientHeight - box.offsetHeight;
    44. // 处理边界
    45. if (left < 0) left = 0;
    46. if (left > width) left = width;
    47. if (top < 0) top = 0;
    48. if (top > height) top = height;
    49. box.style.left = `${left}px`;
    50. box.style.top = `${top}px`;
    51. }
    52. function handleStop() {
    53. document.onmouseup = null
    54. document.onmousemove = null
    55. }
    56. </script>