背景
分享一波LeetCode经典的 linked-list 相关的题目,都是比较简单的
面试题 02.01. 移除重复节点
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
/*
利用哈希表来解决这个问题
*/
var removeDuplicateNodes = function(head) {
if (head === null) {
return head;
}
const mySet = new Set();
mySet.add(head.val);
let prev = head;
let cur = head.next;
while (cur) {
if (mySet.has(cur.val)) {
/* 删除当前节点 */
prev.next = cur.next;
cur = cur.next;
} else {
/* 需要把这个放进去 */
mySet.add(cur.val);
prev = cur;
cur = cur.next;
}
}
return head;
};
面试题 02.06. 回文链表
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
/*
把节点放到数组里
利用双指针进行对比
*/
var isPalindrome = function(head) {
const arr = [];
while (head) {
arr.push(head.val);
head = head.next;
}
for (let i = 0, j = arr.length - 1; i < j; i++,j--) {
if (arr[i] !== arr[j]) {
return false;
}
}
return true;
};
面试题 02.07. 链表相交
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function(headA, headB) {
if (headA === null || headB === null) return null;
let nodeA = headA;
let nodeB = headB;
while (nodeA !== nodeB) {
nodeA = nodeA == null ? headB : nodeA.next;
nodeB = nodeB == null ? headA : nodeB.next;
}
return nodeA;
};
面试题 02.03. 删除中间节点
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} node
* @return {void} Do not return anything, modify node in-place instead.
*/
/*
这个非常巧妙
相当于自己这个节点被下一个节点替换了
*/
var deleteNode = function(node) {
node.val = node.next.val;
node.next = node.next.next;
};
面试题 02.02. 返回倒数第 k 个节点
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {number}
*/
/* 快慢指针的方式
快指针先走K步,然后,再一起走
*/
var kthToLast = function(head, k) {
if (!head) return head;
let prev = new ListNode(-1);
prev.next = head;
let cur = prev;
let slow = head;
while (k !== 0) {
cur = cur.next;
k--;
}
while (cur.next) {
cur = cur.next;
slow = slow.next;
}
return slow.val;
};
剑指 Offer 06. 从尾到头打印链表
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {number[]}
*/
var reversePrint = function(head) {
if (!head) return [];
const arr = [];
while (head) {
arr.unshift(head.val);
head = head.next;
}
return arr;
};
剑指 Offer 25. 合并两个排序的链表
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var mergeTwoLists = function(l1, l2) {
let res = new ListNode(-1);
let cur = res;
let [p1, p2] = [l1, l2];
/* 当两个都可以迭代的时候 */
while (p1 && p2) {
if (p1.val > p2.val) {
cur.next = p2;
p2 = p2.next;
} else {
cur.next = p1;
p1 = p1.next;
}
cur = cur.next;
}
/* 迭代完成其中一个了, 把剩下的拼起来 */
cur.next = p1 ? p1 : p2;
return res.next;
};
剑指 Offer 22. 链表中倒数第k个节点
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
/* 快慢指针 */
var getKthFromEnd = function(head, k) {
if (!head) return head;
/* 先走K个结点 */
let prev = new ListNode(-1);
prev.next = head;
let fast = prev;
let slow = head;
while (k !== 0) {
fast = fast.next;
k--;
}
/* 快慢一起走 */
while (fast.next) {
fast = fast.next;
slow = slow.next;
}
return slow;
};
剑指 Offer 24. 反转链表
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
let prev = null;
let cur = head;
if (!head) return head;
while (cur) {
let tmp = cur.next;
cur.next = prev;
prev = cur;
cur = tmp;
}
return prev;
};
剑指 Offer 18. 删除链表的节点
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
var deleteNode = function(head, val) {
if (!head) return head;
let cur = head;
let prev = new ListNode(-1);
prev.next = head;
let res = prev;
while (cur) {
if (cur.val === val) {
prev.next = cur.next;
}
let tmp = cur.next;
prev = cur;
cur = tmp;
}
return res.next;
};
剑指 Offer 52. 两个链表的第一个公共节点
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function(headA, headB) {
if (headA === null || headB === null) return null;
let pA = headA;
let pB = headB;
while (pA !== pB) {
pA = pA === null ? headB : pA.next;
pB = pB === null ? headA : pB.next;
}
return pA;
};
剑指 Offer II 023. 两个链表的第一个重合节点
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
/* 双指针 */
var getIntersectionNode = function(headA, headB) {
if (headA === null || headB === null) {
return null;
}
let nodeA = headA;
let nodeB = headB;
while (nodeA !== nodeB) {
nodeA = nodeA === null ? headB : nodeA.next;
nodeB = nodeB === null ? headA : nodeB.next;
}
return nodeA;
};
剑指 Offer II 024. 反转链表
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
/* 将要返回的头节点 */
let prev = null;
let cur = head;
while (cur) {
/* 下一个节点先存起来 */
let tmp = cur.next;
cur.next = prev;
prev = cur;
cur = tmp;
}
return prev;
};
剑指 Offer II 027. 回文链表
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var isPalindrome = function(head) {
/* 把节点放在数组里 */
const arr = [];
while (head) {
arr.push(head.val);
head = head.next;
}
/* for */
for(let i = 0, j = arr.length - 1; i < j; i++, j--) {
if (arr[i] !== arr[j]) {
return false
}
}
return true;
};