鏈表

鏈表是一種鏈式存儲的線性表,所有元素的內存地址不一定是連續(xù)的。
在編寫鏈表過程中,要注意邊界測試,比如 index 為 0 size -0 size 時

刪除鏈表中的節(jié)點

輸入: head = [4,5,1,9], node = 5
輸出: [4,1,9]
解釋: 給定你鏈表中值為 5 的第二個節(jié)點,那么在調用了你的函數之后,該鏈表應變?yōu)?4 -> 1 -> 9.

var deleteNode = function(node) {
var afterNode = node.next;
node.val=afterNode.val;
node.next=afterNode.next
};

反轉一個單鏈表
遞歸

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;
}
}

判斷一個鏈表是否有環(huán)

輸入:head = [3,2,0,-4], pos = 1
輸出:true
解釋:鏈表中有一個環(huán),其尾部連接到第二個節(jié)點。

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
解釋:鏈表中有一個環(huán),其尾部連接到第二個節(jié)點。

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;
}
}

兩數相加
給出兩個 非空 的鏈表用來表示兩個非負的整數。其中,它們各自的位數是按照 逆序 的方式存儲的,并且它們的每個節(jié)點只能存儲 一位 數字。

如果,我們將這兩個數相加起來,則會返回一個新的鏈表來表示它們的和。

您可以假設除了數字 0 之外,這兩個數都不會以 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;

};

給定一個鏈表,刪除鏈表的倒數第 n 個節(jié)點,并且返回鏈表的頭結點。

示例:

給定一個鏈表: 1->2->3->4->5, 和 n = 2.

當刪除了倒數第二個節(jié)點后,鏈表變?yōu)?1->2->3->5.

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list
著作權歸領扣網絡所有。商業(yè)轉載請聯系官方授權,非商業(yè)轉載請注明出處。

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;

};

合并兩個有序鏈表
將兩個有序鏈表合并為一個新的有序鏈表并返回。新鏈表是通過拼接給定的兩個鏈表的所有節(jié)點組成的。

示例:

輸入:1->2->4, 1->3->4
輸出:1->1->2->3->4->4

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/merge-two-sorted-lists
著作權歸領扣網絡所有。商業(yè)轉載請聯系官方授權,非商業(yè)轉載請注明出處。

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;

};

  1. 兩兩交換鏈表中的節(jié)點
    給定一個鏈表,兩兩交換其中相鄰的節(jié)點,并返回交換后的鏈表。

你不能只是單純的改變節(jié)點內部的值,而是需要實際的進行節(jié)點交換。

示例:

給定 1->2->3->4, 你應該返回 2->1->4->3.

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/swap-nodes-in-pairs
著作權歸領扣網絡所有。商業(yè)轉載請聯系官方授權,非商業(yè)轉載請注明出處。

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;

};

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容