# 01 - 链表

18 阅读1分钟

1、反转链表

``````var reverseList = function (head) {
let prev = null, cur = head;
while (cur) {
// [cur.next, prev, cur] = [prev, cur, cur.next];
let temp = cur.next;
cur.next = prev;
prev = cur;
cur = temp;
}
return prev
}
``````
• 给出的链表为 1→2→3→4→5→NULL, m=2,n=4,
• 返回 1→4→3→2→5→NULL.
``````    const dummy_node = new ListNode(-1);

let pre = dummy_node;
for (let i = 0; i < m - 1; ++i) pre = pre.next;

let cur = pre.next;
for (let i = 0; i < n - m; ++i) {
[pre.next, cur.next, pre.next.next] = [cur.next, cur.next.next, pre.next];
}
return dummy_node.next;
};
``````
• 给定的链表是 1→2→3→4→5
• 对于 k=2 , 你应该返回 2→1→4→3→5
• 对于 k=3 , 你应该返回 3→2→1→4→5
``````function reverseKGroup(head, k) {
let pre = null,
for (let i = 0; i < k; i++) {
if (node === null) return head;
node = node.next;
}
for (let j = 0; j < k; j++) {
[cur.next, pre, cur] = [pre, cur, cur.next];
}
return pre;
}
``````
``````function Merge(pHead1, pHead2) {

} else {
}
}
``````
``````// 使用快慢指针
var hasCycle = function (head) {
while (fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) return true;
}
return false;
};
``````
``````var detectCycle = function (head) {
let cache = new Set();
} else {
}
}
return null;
};
``````
• 输入：{ 1, 2, 3, 4, 5 }, 2
• 返回值：{ 4, 5 }
``````let FindKthToTail = function (pHead, k) {
for (let i = 0; i < k; i++) {
if (!fast) return null;
fast = fast.next;
}
while (fast) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
``````
``````let removeNthFromEnd = function (head, n) {
for (let i = 0; i < n; i++) {
fast = fast.next;
}

let pre;
while (fast) {
fast = fast.next;
pre = slow;
slow = slow.next;
}
pre.next = slow.next;
}
``````
``````function FindFirstCommonNode(headA, headB) {
while (a != b) {
a = a ? a.next : headB;
b = b ? b.next : headA;
}
return a || b;
};

// 执行过程
--> [1, 2, 3, 6, 7][4, 5, 6, 7]
--> [2, 3, 6, 7][5, 6, 7]
--> [3, 6, 7][6, 7]
--> [6, 7][7]
--> [7] null
--> null[1, 2, 3, 6, 7]
--> [4, 5, 6, 7][2, 3, 6, 7]
--> [5, 6, 7][3, 6, 7]
``````
``````function isPalindrome(head) {
let positiveStr = ``, reverseStr = ``;

positiveStr = positiveStr + val;
reverseStr = val + reverseStr;
}

return positiveStr === reverseStr;
};
``````

11、链表的奇偶重排

• 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL
• 重排后为
• 1 -> 3 -> 5 -> 2 -> 4 -> 6 -> NULL
``````function oddEvenList(head) {
while (even && even.next) {
odd.next = odd.next.next;
even.next = even.next.next;
odd = odd.next;
even = even.next;
}
}
``````
• 给出的链表为1→1→2, 返回1→2.
• 给出的链表为1→1→2→3→3, 返回1→2→3.
``````var deleteDuplicates = function (head) {
while (cur && cur.next) {
if (cur.val == cur.next.val) {
cur.next = cur.next.next;
} else {
cur = cur.next;
}
}
};
``````
``````function deleteDuplicates(head) {

let res = new ListNode(-1);
let prev = res, cur = res.next;

while (cur && cur.next) {
if (cur.val == cur.next.val) {
while (cur.val == cur.next.val) {
cur.next = cur.next.next;
if (!cur.next) break;
}
prev.next = cur.next;
} else {
prev = cur;
}
cur = cur.next
}
return res.next;
}
``````