192 lines
8.0 KiB
HTML
192 lines
8.0 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="zh-Hans">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>023. 反转链表 – 图解</title>
|
||
<link rel="stylesheet" href="../shared/style.css">
|
||
<style>
|
||
/* page-specific overrides */
|
||
.vis-area { min-height: 120px; padding: 16px 0; }
|
||
.code-section { margin-top: 16px; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<h1>🟢 023. 反转链表 <span class="badge easy">简单</span></h1>
|
||
<p class="subtitle">分类:链表 | LeetCode Hot 100</p>
|
||
|
||
<!-- 控制面板 -->
|
||
<div class="controls" id="controls">
|
||
<label for="inputArea">输入:</label>
|
||
<input type="text" id="inputArea" placeholder="默认示例,可自定义">
|
||
<button id="applyBtn" class="primary">生成图解</button>
|
||
<select id="exampleSelect"></select>
|
||
<span style="flex:1"></span>
|
||
<button id="prevBtn">◀ 上一步</button>
|
||
<button id="nextBtn">下一步 ▶</button>
|
||
<button id="jumpBtn">⏭ 跳到结果</button>
|
||
<button id="autoBtn">自动播放</button>
|
||
<button id="resetBtn">重置</button>
|
||
</div>
|
||
|
||
<!-- 步骤流水线 -->
|
||
<div class="pipeline" id="pipeline"></div>
|
||
|
||
<!-- 提示条 -->
|
||
<div class="hint info" id="hintBox">
|
||
<span id="stepInfo"></span><br>
|
||
<span id="hintText"></span>
|
||
</div>
|
||
|
||
<!-- 可视化面板 -->
|
||
<div class="panels">
|
||
<div class="panel" id="mainPanel">
|
||
<h3>📊 可视化</h3>
|
||
<div class="vis-area" id="vizArea">点击「生成图解」开始</div>
|
||
</div>
|
||
<div class="panel-grid">
|
||
<div class="panel" id="detailPanel">
|
||
<h3>📝 当前步骤详情</h3>
|
||
<div id="detailContent">等待开始...</div>
|
||
</div>
|
||
<div class="panel" id="resultPanel">
|
||
<h3>✅ 结果</h3>
|
||
<div id="resultContent">等待完成...</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 代码 -->
|
||
<div class="panel code-section">
|
||
<h3>💻 参考代码(Python)</h3>
|
||
<div id="codeArea"></div>
|
||
</div>
|
||
|
||
<footer>Powered by QwenPaw · 图解算法 · LeetCode Hot 100</footer>
|
||
</div>
|
||
|
||
<script src="../shared/algo-viz.js"></script>
|
||
<script>
|
||
"use strict";
|
||
(function() {
|
||
// ========== Algorithm Logic ==========
|
||
|
||
function llViz(nodes, opts) {
|
||
opts = opts || {};
|
||
var highs = opts.highs || {};
|
||
var ptrs = opts.ptrs || {};
|
||
var cycle = opts.cycle != null ? opts.cycle : -1;
|
||
var label = opts.label || '';
|
||
var showNull = opts.showNull !== false;
|
||
var html = '';
|
||
if (label) html += '<div style="font-size:13px;font-weight:600;color:var(--text-secondary);margin-bottom:4px;">' + label + '</div>';
|
||
html += '<div style="display:flex;flex-wrap:wrap;align-items:center;gap:4px;padding:4px 0;">';
|
||
for (var i = 0; i < nodes.length; i++) {
|
||
var cls = highs[i] || '';
|
||
html += '<div style="position:relative;display:inline-flex;flex-direction:column;align-items:center;">';
|
||
html += '<div class="ll-node ' + cls + '">';
|
||
html += '<span class="val">' + nodes[i] + '</span>';
|
||
if (i === cycle) {
|
||
html += '<span class="arrow" style="color:var(--red);font-weight:bold;">↩</span>';
|
||
} else if (i < nodes.length - 1) {
|
||
html += '<span class="arrow">→</span>';
|
||
} else if (showNull && i === nodes.length - 1) {
|
||
html += '<span class="arrow" style="color:var(--text-muted);">∅</span>';
|
||
}
|
||
html += '</div>';
|
||
var plabels = [];
|
||
var pkeys = Object.keys(ptrs);
|
||
for (var pi = 0; pi < pkeys.length; pi++) {
|
||
if (ptrs[pkeys[pi]] === i) plabels.push(pkeys[pi]);
|
||
}
|
||
if (plabels.length) {
|
||
html += '<div style="font-size:10px;font-weight:700;color:var(--blue);margin-top:2px;white-space:nowrap;">' + plabels.join(',') + '</div>';
|
||
}
|
||
html += '</div>';
|
||
}
|
||
html += '</div>';
|
||
return html;
|
||
}
|
||
|
||
function setPipeline(stages, current) {
|
||
$('pipeline').innerHTML = stages.map(function(st) {
|
||
var parts = st.split('\u2192');
|
||
return '<span class="pipe-step ' + (current===parts[0]?'active':'') + '">' + parts[1] + '</span>';
|
||
}).join('<i>\u2192</i>');
|
||
}
|
||
|
||
var examples = [
|
||
{input:[1,2,3,4,5], label:'示例1: [1,2,3,4,5]'},
|
||
{input:[1,2], label:'示例2: [1,2]'},
|
||
{input:[], label:'示例3: []'},
|
||
];
|
||
var nodes, steps, stepCtrl;
|
||
|
||
function buildSteps(arr) {
|
||
nodes = arr.slice(); steps = [];
|
||
steps.push({stage:'init', msg:'迭代法:prev=null, curr=头节点, 每步将 curr.next 指向 prev', prev:-1, curr:0, next:1});
|
||
var prev = -1, curr = 0;
|
||
while (curr < arr.length) {
|
||
var nxt = curr + 1;
|
||
steps.push({stage:'visit', msg:'访问 curr=' + arr[curr] + ':保存 next=' + (nxt<arr.length?arr[nxt]:'null') + ',将 ' + arr[curr] + '.next 指向 ' + (prev>=0?arr[prev]:'null'), prev:prev, curr:curr, next:nxt});
|
||
prev = curr;
|
||
curr = nxt;
|
||
steps.push({stage:'shift', msg:'prev 移到 ' + arr[prev] + ',curr 移到 ' + (curr<arr.length?arr[curr]:'null'), prev:prev, curr:curr, next:curr});
|
||
}
|
||
steps.push({stage:'done', msg:'反转完成,新头节点为 ' + (prev>=0?arr[prev]:'null'), prev:prev, curr:-1, next:-1});
|
||
}
|
||
|
||
function render(step) {
|
||
var s = steps[step];
|
||
var hl = {};
|
||
if (s.curr >= 0 && s.curr < nodes.length) hl[s.curr] = 'current';
|
||
if (s.prev >= 0 && s.prev < nodes.length) hl[s.prev] = 'active';
|
||
if (s.next < nodes.length && s.next >= 0) hl[s.next] = 'null-node';
|
||
// Show reversed portion in green, unreversed in blue
|
||
for (var i = 0; i < nodes.length; i++) {
|
||
if (s.prev >= 0 && i <= s.prev && i > s.curr) {
|
||
hl[i] = 'active';
|
||
}
|
||
}
|
||
var viz = llViz(nodes, {highs:hl, ptrs:{prev:s.prev>=0&&s.prev<nodes.length?s.prev:-1, curr:s.curr>=0&&s.curr<nodes.length?s.curr:-1, next:s.next<nodes.length&&s.next>=0?s.next:-1}, label:'链表状态'});
|
||
if (s.prev >= 0) {
|
||
var reversed = [];
|
||
for (var i = s.prev; i >= 0; i--) reversed.push(nodes[i]);
|
||
viz += llViz(reversed, {highs:{}, label:'已反转部分', showNull:false});
|
||
}
|
||
$('vizArea').innerHTML = viz;
|
||
$('detailContent').innerHTML = '<div class="calc-block">' + s.msg + '</div>';
|
||
if (s.stage === 'done') {
|
||
var result = [];
|
||
for (var i = nodes.length-1; i >= 0; i--) result.push(nodes[i]);
|
||
$('resultContent').innerHTML = '<div class="final-answer">反转结果:<b>[' + result.join(' → ') + ']</b></div>';
|
||
}
|
||
$('hintText').textContent = s.msg;
|
||
setPipeline(['init\u2192初始化','visit\u2192访问','shift\u2192移动','done\u2192完成'], s.stage);
|
||
}
|
||
|
||
function init() {
|
||
var sel = $('exampleSelect');
|
||
examples.forEach(function(e,i){ sel.innerHTML += '<option value="'+i+'">'+e.label+'</option>'; });
|
||
$('inputArea').value = '[1,2,3,4,5]';
|
||
buildSteps(examples[0].input);
|
||
stepCtrl = new StepController({onStep:render});
|
||
stepCtrl.setSteps(steps.map(function(_,i){return i;}));
|
||
$('stepInfo').textContent = '步骤 1 / ' + steps.length;
|
||
stepCtrl.onStep = function(idx){ render(idx); $('stepInfo').textContent = '步骤 '+(idx+1)+' / '+steps.length; };
|
||
$('applyBtn').onclick = function(){ try { var arr=JSON.parse($('inputArea').value); buildSteps(arr); stepCtrl.setSteps(steps.map(function(_,i){return i;})); render(0); } catch(e){ alert('请输入合法 JSON 数组'); } };
|
||
$('exampleSelect').onchange = function(){ buildSteps(examples[parseInt($('exampleSelect').value)].input); stepCtrl.setSteps(steps.map(function(_,i){return i;})); render(0); };
|
||
$('prevBtn').onclick = function(){ stepCtrl.prev(); };
|
||
$('nextBtn').onclick = function(){ stepCtrl.next(); };
|
||
$('jumpBtn').onclick = function(){ stepCtrl.jumpToEnd(); };
|
||
$('autoBtn').onclick = function(){ var on=stepCtrl.toggleAuto(); $('autoBtn').textContent=on?'暂停':'自动播放'; };
|
||
$('resetBtn').onclick = function(){ stepCtrl.reset(); $('autoBtn').textContent='自动播放'; };
|
||
}
|
||
init();
|
||
$('codeArea').innerHTML = renderCode('def reverseList(head):\n prev, curr = None, head\n while curr:\n nxt = curr.next\n curr.next = prev\n prev = curr\n curr = nxt\n return prev', {lang:'Python'});
|
||
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html> |