This is the 26th day of my participation in the August Text Challenge.More challenges in August

JavaScript linked list (2), JavaScript linked list (3), JavaScript linked list (4)

Interested in children’s shoes can be rushed, then continue to brush two links list questions ~~

Delete duplicate elements from sorted list

83. Delete duplicate elements from sorted linked lists

There is a linked list in ascending order. Given the head node of the linked list, remove all duplicate elements so that each element appears only once.

Returns a linked list of results, also in ascending order.

Example 1:

Enter: head = [1.1.2] output: [1.2]
Copy the code

Example 2:

Enter: head = [1.1.2.3.3] output: [1.2.3]
Copy the code

Tip:

  • The number of nodes in the linked list is in the range [0, 300]
  • -100 <= Node.val <= 100
  • The subject data ensures that the linked list is sorted in ascending order

Source: LeetCode link: leetcode-cn.com/problems/re… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

Their thinking

Set a pointer and go backwards until you reach the end of the list if the current node is equal to the next node.

Code implementation

/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */
/ * * *@param {ListNode} head
 * @return {ListNode}* /
var deleteDuplicates = function(head) {
    let cur = head;
    while(cur ! =null&& cur.next ! =null) {
        if(cur.val == cur.next.val) {
             cur.next = cur.next.next;
        } else{ cur = cur.next; }}return head;
};
Copy the code

Separate linked lists

86. Separate linked lists

Given a list of head nodes and a specific value x, separate the list so that all nodes less than x appear before nodes greater than or equal to x.

You should preserve the initial relative position of each node in the two partitions.

Example 1:

Input: head = [1,4,3,2,5,2], x = 3 output: [1,2,2,4,3,5]Copy the code

Example 2:

Input: head = [2,1], x = 2Copy the code

Tip:

  • The number of nodes in the linked list is in the range [0, 200]
  • -100 <= Node.val <= 100
  • -200 <= x <= 200

Source: LeetCode link: leetcode-cn.com/problems/pa… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

Their thinking

Set two linked lists, small and BIG. Take out the nodes in the original linked list one by one, judge their size and put them into the corresponding linked list.

When all the nodes are in place, link the tail of the small list to the head of the big list, thus forming the desired list.

Code implementation

/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */
/ * * *@param {ListNode} head
 * @param {number} x
 * @return {ListNode}* /
var partition = function(head, x) {
    // Set two lists
    let small = new ListNode();
    let big = new ListNode();
    // Two linked list operation Pointers
    let bigNode= big;
    let smallNode = small;
    // Set p to the current node
    // Set q to point to the next node of p, and set p.ext to null to make it a separate node
    // Determine the size of p and place p in the corresponding list
    for(letp = head; p ! =null; p = q) {
            q = p.next;
            p.next = null;
        if(p.val >= x) {
            bigNode.next = p;
            bigNode = bigNode.next;
        } else {
            smallNode.next = p;
            smallNode = smallNode.next;
        }
    }
    smallNode.next = big.next;
    return small.next;
};
Copy the code