鏈表是一種鏈?zhǔn)酱鎯Φ木€性表,所有元素的內(nèi)存地址不一定是連續(xù)的。
在編寫鏈表過程中,要注意邊界測試,比如 index 為 0 size -0 size 時(shí)
刪除鏈表中的節(jié)點(diǎn)
輸入: head = [4,5,1,9], node = 5
輸出: [4,1,9]
解釋: 給定你鏈表中值為 5 的第二個(gè)節(jié)點(diǎn),那么在調(diào)用了你的函數(shù)之后,該鏈表應(yīng)變?yōu)?4 -> 1 -> 9.
var deleteNode = function(node) {
var afterNode = node.next;
node.val=afterNode.val;
node.next=afterNode.next
};
反轉(zhuǎn)一個(gè)單鏈表
遞歸
var reverseList = function(head){
if (head == null || head.next == null) return head;
let p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
迭代頭插法
var reverseList = function(head){
let prev = null;
let curr = head;
while(curr !=null){
let netxTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
}
判斷一個(gè)鏈表是否有環(huán)
輸入:head = [3,2,0,-4], pos = 1
輸出:true
解釋:鏈表中有一個(gè)環(huán),其尾部連接到第二個(gè)節(jié)點(diǎn)。
var hasCycle = function(head) {
if(head==null)return false;
let fastNode = head.next;
let lowNode = head;
while(fastNode!=lowNode){
if (fastNode == null || fastNode.next == null) {
return false;
}
fastNode = fastNode.next.next;
lowNode = lowNode.next;
}
return true;
}
環(huán)形鏈表 II
輸入:head = [3,2,0,-4], pos = 1
輸出:tail connects to node index 1
解釋:鏈表中有一個(gè)環(huán),其尾部連接到第二個(gè)節(jié)點(diǎn)。
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while (true) {
if (fast == null || fast.next == null) return null;
fast = fast.next.next;
slow = slow.next;
if (fast == slow) break;
}
fast = head;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
}
return fast;
}
}
兩數(shù)相加
給出兩個(gè) 非空 的鏈表用來表示兩個(gè)非負(fù)的整數(shù)。其中,它們各自的位數(shù)是按照 逆序 的方式存儲的,并且它們的每個(gè)節(jié)點(diǎn)只能存儲 一位 數(shù)字。
如果,我們將這兩個(gè)數(shù)相加起來,則會返回一個(gè)新的鏈表來表示它們的和。
您可以假設(shè)除了數(shù)字 0 之外,這兩個(gè)數(shù)都不會以 0 開頭。
示例:
輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
輸出:7 -> 0 -> 8
原因:342 + 465 = 807
var addTwoNumbers = function(l1, l2) {
let result = new ListNode(null);
let nextRst = result;
let isAdd=false;
while(l1!=null||l2!=null){
let sum = (l1?l1.val:0)+(l2?l2.val:0)+(isAdd?1:0);
if (sum>=10) {
isAdd=true;
}else{
isAdd=false;
}
nextRst.next = new ListNode(sum%10);
nextRst = nextRst.next
if(l1 != null) l1 = l1.next
if(l2 != null) l2 = l2.next
}
if(isAdd){
nextRst.next = new ListNode(1);
}
return result.next;
};
給定一個(gè)鏈表,刪除鏈表的倒數(shù)第 n 個(gè)節(jié)點(diǎn),并且返回鏈表的頭結(jié)點(diǎn)。
示例:
給定一個(gè)鏈表: 1->2->3->4->5, 和 n = 2.
當(dāng)刪除了倒數(shù)第二個(gè)節(jié)點(diǎn)后,鏈表變?yōu)?1->2->3->5.
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
var removeNthFromEnd = function(head, n) {
let count = 0;
let newHead = head;
let ary =[];
while(head!=null){
ary.push(head);
head=head.next;
}
if(n==1){
let node = ary[ary.length>1?ary.length-n-1:ary.length-n];
if(node.next){
node.next= null
}else{
newHead=null;
}
}else{
let deleteNode = ary[ary.length-n];
let nextNode = deleteNode.next;
deleteNode.next = nextNode?nextNode.next:null;
deleteNode.val = nextNode?nextNode.val:null;
}
return newHead;
};
合并兩個(gè)有序鏈表
將兩個(gè)有序鏈表合并為一個(gè)新的有序鏈表并返回。新鏈表是通過拼接給定的兩個(gè)鏈表的所有節(jié)點(diǎn)組成的。
示例:
輸入:1->2->4, 1->3->4
輸出:1->1->2->3->4->4
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/merge-two-sorted-lists
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
var mergeTwoLists = function(l1, l2) {
let newHead = new ListNode(null);
let head = newHead;
while(l1!=null&&l2!=null){
if(l1.val<=l2.val){
head.next=l1;
l1=l1.next;
}else{
head.next=l2;
l2=l2.next;
}
head = head.next;
}
head.next=l1==null?l2:l1;
return newHead.next;
};
- 兩兩交換鏈表中的節(jié)點(diǎn)
給定一個(gè)鏈表,兩兩交換其中相鄰的節(jié)點(diǎn),并返回交換后的鏈表。
你不能只是單純的改變節(jié)點(diǎn)內(nèi)部的值,而是需要實(shí)際的進(jìn)行節(jié)點(diǎn)交換。
示例:
給定 1->2->3->4, 你應(yīng)該返回 2->1->4->3.
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/swap-nodes-in-pairs
著作權(quán)歸領(lǐng)扣網(wǎng)絡(luò)所有。商業(yè)轉(zhuǎn)載請聯(lián)系官方授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
var swapPairs = function(head) {
let dummy = new ListNode(-1);
dummy.next = head;
let prevNode = dummy;
while ((head != null) && (head.next != null)) {
// Nodes to be swapped
let firstNode = head;
let secondNode = head.next;
// Swapping
prevNode.next = secondNode;
firstNode.next = secondNode.next;
secondNode.next = firstNode;
// Reinitializing the head and prevNode for next swap
prevNode = firstNode;
head = firstNode.next; // jump
}
return dummy.next;
};