链表算法题

链表算法题

链表算法题( JavaScript 实现)

反转链表

1
2
3
4
5
6
7
8
9
10
11
12
function reverseList(pHead)
{
let prev = null;
let curr = pHead;
while (curr) {
const next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}

两两交换链表中的节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var swapPairs = function(head) {
let dummyNode = new ListNode(0);
dummyNode.next = head;
let temp = dummyNode;
while (temp.next != null && temp.next.next != null) {
let head1 = temp.next;
let head2 = temp.next.next;
temp.next = head2;
head1.next = head2.next;
head2.next = head1;
temp = head1;
}
return dummyNode.next;
};

判断链表中是否有环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function hasCycle( head ) {
if (head == null) {
return false;
}
let slow = head;
let fast = head;
while (fast.next && fast.next.next) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true;
}
}
return false;
}

环的入口结点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function detectCycle( head ) {
if (!head) return null;
let slow = head, fast = head;
while(fast.next && fast.next.next) {
fast = fast.next.next
slow = slow.next
if (slow == fast) {
var a = head;
while (slow != a) {
a = a.next
slow = slow.next
}
return a;
}
}
return null;
}

链表中倒数第 k 个结点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function FindKthToTail( pHead ,  k ) {
let length = getLength(pHead);
if (length < k) {
return null;
}
let i = length - k;
let p = pHead;
while (i > 0) {
p = p.next;
i--;
}
return p;
}

var getLength = function( pHead ) {
let p = pHead;
let length = 0;
while (p != null) {
p = p.next;
length++;
}
return length;
}

删除链表的倒数第 n 个节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function removeNthFromEnd( head ,  n ) {
let length = getLength(head);
if (length < n) {
return null;
}
let i = length - n;
let dummy = new ListNode(0);
dummy.next = head;
let p = dummy;
while (i > 0) {
p = p.next;
i--;
}
p.next = p.next.next;
return dummy.next;
}

var getLength = function ( head ) {
let length = 0;
let p = head;
while (p != null) {
p = p.next;
length ++;
}
return length;
}

删除有序链表中重复的元素

1
2
3
4
5
6
7
8
9
10
11
function deleteDuplicates( head ) {
let p = head;
while (p != null && p.next != null) {
if (p.val == p.next.val) {
p.next = p.next.next;
} else {
p = p.next;
}
}
return head;
}

交叉链表找交点

1
2
3
4
5
6
7
8
9
10
11
12
function FindFirstCommonNode(pHead1, pHead2)
{
if (pHead1 == null || pHead2 == null) {
return null;
}
let p1 = pHead1, p2 = pHead2;
while (p1 != p2) {
p1 = p1 == null ? pHead2 : p1.next;
p2 = p2 == null ? pHead1 : p2.next;
}
return p1;
}

合并两个有序链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function mergeTwoLists( l1 ,  l2 ) {
let newHead = new ListNode(-1);
let p = newHead;
while(l1 != null && l2 != null) {
if (l1.val < l2.val) {
p.next = l1;
l1 = l1.next;
} else {
p.next = l2;
l2 = l2.next;
}
p = p.next;
}
if (l1 != null) {
p.next = l1;
}
if (l2 != null) {
p.next = l2;
};
return newHead.next;
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×