https://leetcode-cn.com/problems/intersection-of-two-linked-lists/
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
// a:1->2->3->4->5->6->null->9->5->6->null
// b:9->5->6->null->1->2->3->4->5->6->null
function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null {
if (!headA || !headB) return null;
let a = headA, b = headB;
while (a !== b) {
a = a ? a.next : headB;
b = b ? b.next : headA
}
return a;
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>拖拽的DIV</title>
</head>
<body>
<div id="box"></div>
</body>
</html>
<style>
* {
margin: 0;
padding: 0;
}
#box {
position: absolute;
left: 50%;
top: 50%;
width: 60px;
height: 60px;
border-radius: 50%;
background: blueviolet;
cursor: move;
}
</style>
<script>
const box = document.getElementById('box');
let offsetX, offsetY;
box.onmousedown = function (e) {
const { left, top } = box.getBoundingClientRect();
offsetX = e.clientX - left;
offsetY = e.clientY - top;
document.onmousemove = handleMove;
document.onmouseup = handleStop;
}
function handleMove(e) {
let left = e.clientX - offsetX;
let top = e.clientY - offsetY;
const width = document.documentElement.clientWidth - box.offsetWidth;
const height = document.documentElement.clientHeight - box.offsetHeight;
// 处理边界
if (left < 0) left = 0;
if (left > width) left = width;
if (top < 0) top = 0;
if (top > height) top = height;
box.style.left = `${left}px`;
box.style.top = `${top}px`;
}
function handleStop() {
document.onmouseup = null
document.onmousemove = null
}
</script>