hon9g / algorithms

TIL: to keep practice on algorithms
6 stars 2 forks source link

Linked List #27

Open hon9g opened 4 years ago

hon9g commented 4 years ago

Problems selected by LeetCode for the topic Linked List 🌐

Click ✔️ to go to each solution. The Link to each problem🌐 is at the top of each solution.

🔗 Singly Linked List

# title my solution
707 Design Linked List ✔️

🔗 Two Pointer Technique

# title my solution
141 Linked List Cycle ✔️
142 Linked List Cycle II ✔️
160 Intersection of Two Linked Lists ✔️
19 Remove Nth Node From End of List ✔️

🔗 Classic Problems

# title my solution
206 Reverse Linked List ✔️
203 Remove Linked List Elements ✔️
328 Odd Even Linked List ✔️
234 Palindrome Linked List ✔️

🔗 Doubly Linked List

# title my solution
707 Design Linked List ✔️

🔗 Conclusion

# title my solution
21 Merge Two Sorted Lists ✔️
2 Add Two Numbers ✔️
430 Flatten a Multilevel Doubly Linked List ✔️
708 Insert into a Cyclic Sorted List ✔️
138 Copy List with Random Pointer ✔️
61 Rotate List ✔️
hon9g commented 4 years ago

707. Design Linked List

/**

/**

/**

/**

/**

complexity analysis

/**
 * Node Constructor
 */
const Node = function(val=null, prev=null, next=null) {
    this.val = val;
    this.prev = prev;
    this.next = next;
};

/**
 * Linked List Constructor
 */
const MyLinkedList = function() {
    this.length = 0;
    this.head = new Node();
    this.tail = new Node();
    this.head.next = this.tail;
    this.tail.prev = this.head;
};

/**
 * Get the value of the index-th node in the linked list. If the index is invalid, return -1. 
 * @param {number} i
 * @return {number}
 */
MyLinkedList.prototype.get = function(i) {
    if (i < 0 || i >= this.length) return -1;
    if (i + 1 === this.length) return this.tail.prev.val;
    let curr = this.head.next;
    while (i--) curr = curr.next;
    return curr.val;
};

/**
 * Add a node of value val before the first element of the linked list.
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function(val) {
    this.length++;
    const nextNode = this.head.next;
    const node = new Node(val, this.head, nextNode);
    this.head.next = node, nextNode.prev = node;
};

/**
 * Append a node of value val to the last element of the linked list. 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    this.length++;
    const prevNode = this.tail.prev;
    const node = new Node(val, prevNode, this.tail);
    this.tail.prev = node, prevNode.next = node;
};

/**
 * Add a node of value val before the i-th node in the linked list.
 * @param {number} i
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(i, val) {
    if (i < 0 || this.length < i) return;
    if (i === this.length) {
        this.addAtTail(val);
        return;
    }
    this.length++;
    let curr = this.head;
    while (i--) curr = curr.next;
    const nextNode = curr.next;
    const node = new Node(val, curr, nextNode);
    curr.next = node, nextNode.prev = node;
};

/**
 * Delete the i-th node in the linked list, if the index is valid. 
 * @param {number} i
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function(i) {
    if (this.length <= i || i < 0) return;
    this.length--;
    if (i === this.length) {
        this.tail.prev = this.tail.prev.prev;
        return;
    }
    let curr = this.head;
    while(i--) curr = curr.next;
    curr.next = curr.next.next;
};
hon9g commented 4 years ago

141. Linked List Cycle

hon9g commented 4 years ago

142. Linked List Cycle II

hon9g commented 4 years ago

160. Intersection of Two Linked Lists

  1. For each iteration, move the tortoise pointer once and move the hare pointer twice to find the intersection. If there is no cycle, the hare pointer will be null and exit the loop.

  2. If there are intersected nodes in the A+B list, move the head and intersected node with same speed in A+B list to find the node where the cycle begins. (If there are no nodes intersected, this step will not run.)

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
const getIntersectionNode = (headA, headB) => {
    let tortoise = headA, hare = headA, curr = headA;
    while (curr) {
        if (!curr.next) {
            curr.next = headB;
            break;
        }
        curr = curr.next;
    }
    while (tortoise && hare) {
        tortoise = tortoise.next;
        hare = hare.next ? hare.next.next : null;
        if (tortoise === hare) {
            break;
        }
    }
    while (headA && hare) {
        if (headA === hare) {
            break;
        }
        headA = headA.next;
        hare = hare.next;
    }
    if (curr) curr.next = null;
    return hare;
};

Different Counts of Nodes

/**

hon9g commented 4 years ago

19. Remove Nth Node From End of List

edge case n = 3 linked list = [1,2,3]

complexity

hon9g commented 4 years ago

206. Reverse Linked List

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
const reverseList = (head, prev=null) => {
    if (!head) return prev;
    [head.next, prev, head] = [prev, head, head.next]; 
    return reverseList(head, prev);
};
hon9g commented 4 years ago

203. Remove Linked List Elements

hon9g commented 4 years ago

328. Odd Even Linked List

hon9g commented 4 years ago

234. Palindrome Linked List

hon9g commented 4 years ago

21. Merge Two Sorted Lists

};


```Python
class Solution:
    def mergeTwoLists(self, N, M):
        head = curr = ListNode(0)
        while N and M:
            if N.val < M.val:
                curr.next, N = N, N.next
            else:
                curr.next, M = M, M.next
            curr = curr.next
        curr.next = N or M
        return head.next
hon9g commented 4 years ago

2. Add Two Numbers

hon9g commented 4 years ago

430. Flatten a Multilevel Doubly Linked List

hon9g commented 4 years ago

708. Insert into a Sorted Circular Linked List

hon9g commented 4 years ago

138. Copy List with Random Pointer

hon9g commented 4 years ago

61. Rotate List