Algorithm list title

Single list

function the LinkedList () { 
        // list element needs to be inserted 
        var = the Node function (Element) { 
            this.element = Element; // value of the element 
            this.next = null; // pointer to point to the next entry node         }; 
        var length = 0; // list length 
        (reference) // first node in the list; var head = null 
        // additional element to the tail of the list 
        this.append = function (element) {  var = new new node the node (element), Current;  iF (head === null ) {// if the list is empty = head Node;} the else {// starting from the first element to find = Current head; // circular linked list until it finds the last one while (current.next) {Current = current.next;} // elements inserted into the end of the list current.next =




Node; length} ++ ;}; // remove the element from the list and returns the position of the element function = this.removeAt (position) {IF (position> -1 && position < length) {var = Current head, Previous, index = 0 ; // remove first IF (position == 0 ) {head = current.next; return current.element; the else} {the while (index ++ < position) = {Previous Current; // delete the specified position before element = Current current.next;} = previous.next current.next; length-- ;} return current.element;} the else {return null ;};} // remove elements from the list based on the value this.remove = function (element) {var index = the this .indexOf (element); return the this .removeAt (index);}; // insert an element at an arbitrary position = function this.insert(position, Element) {IF (position> -1 && position <= length) {var = new new Node the Node (Element), Current = head, Previous, index = 0 ; IF (position === 0) {// in Add the first position = node.next Current; head = Node; the else} {the while (index ++ < position) = {Previous Current; Current = current.next;} node.next = Current; previous.next = Node;} length ++ ; return to true ;} the else {return to false ;}}; // to find and return to a position of the element, if the element does not exist, this.indexOf = function returns -1 (element) {var = Current head, index = 0 ; the while (Current) {IF (Element === current.element) {return index; index ++}; Current = current.next;} return -1 ;}; // determines whether the list is empty function = this.isEmpty () {return length === 0 ;}; // return the length of the list function = this.size ( ) {return length;}; // Check the list element value (converted to a string) that this.toString = function () {var = Current head, string = '' ; the while (Current) + = {string "," + current.element; Current = current.next;} return String.slice (. 1 );}; // return the first element of the list function = this.getHead () {return head;}; // list view (in elements and a pointer, an array output) = this.print function () {var = Current head, List = []; the while (Current) {list.push (Current); Current = current.next;} return list; }; } var list = new LinkedList(); list.append(5); console.log(list.toString()); console.log(list.print()); console.log(list.indexOf(115)); console.log(list.isEmpty()); console.log(list.size()); console.log(list.getHead()); console.log(list.removeAt(0)); console.log(list.toString()); console.log(list.removeAt(1)); console.log(list.toString()); list.insert(0, 500); console.log(list.toString());

Delete duplicate the list of nodes

// idea 

special cases: a single linked list, an empty list 
when a new node newHead, placed in front of the head node, the head node needs to be deleted, return results to facilitate 
pre pointer points to a front node (initially newHead) cur point to the current node (initially head node), next points to the next node (initially null) 
cur is not empty, cur.next not empty, enter the circulation, followed by the comparison node, next save cur.next 
If cur and next values are equal, proceeds to the cycle, the sequence Find all after repeating elements, then delete the middle of all duplicate elements (pre.next = next;), cur point next current position 
if cur and next values are not equal, pre and cur turn backward movement, continue to compare 
last traverse quits cycle, the head node returns: newHead.next 

function deleteDuplication (PHEAD) {
   IF (PHEAD pHead.next ||!!) return PHEAD; 
  the let newHead = new new ListNode ( "head"); // Create a node 
  newHead.next = pHead ; // serve as the new head node when the head node is deleted can return the correct head node
  pre = newHead the let; // before a pre node points 
  the let CUR = PHEAD; // CUR current node points to 
  the let next = null ; // the next point to a next node 
  the while (CUR && cur.next) {
     // the current node is not empty and the next node is not empty, the cycle comparator enters 
    next = cur.next; // next storage position of the next node 
    IF (next.val === Cur.Val) {
       // is equal to the next value and cur 
      // into the circulation Find all repeating element back 
      the while (next && next.val === Cur.Val) { 
        next = next.next; // after a next shift 
      }
       // next blank or the next and cur values are not equal, the loop is exited
      = next pre.next; // Remove duplicate intermediate node 
      cur = next; // cur pointer to next position 
    } the else {
       // cur is not equal to the next value and 
      pre = cur; // pre both backward and cur 
      cur = Next; 
    } 
  } 
  return newHead.next; 
}

Palindrome list

 

var isPalindrome = function(head, queue = []) {
  if (!head) {
    return true;
  }
  queue.push(head.val);
  let flag = isPalindrome(head.next, queue);
  return queue.shift() === head.val && flag;
}

Find a single intermediate node list

<! - Find a single list of intermediate nodes: 
Define two nodes k1, k2, k1 moving twice, k2 first step, 
the position of the intermediate node when k2 where k1 come to an end at this time. -> 

<! - Input: [1,2,3,4,5,6 ] 
Output: this list node 4 (SEQ form: [4,5,6 ]) 
Since the list has two intermediate node, values of 3 and 4 , we return to the second node. 
 -> 
 / **  . * Definition List for Singly-linked  * function ListNode (Val) {  * this.val = Val;  * this.next = null;  *}  * / 
   var middleNode = function (head) {  var length = . 1 , Node = head; // list length measuring the while (node.next == null! ) {length ++ ; Node = node.next;} // disposed intermediate the length if (length% 2 === 0) {Length = length / 2 +. 1 ;} the else {length = Math.ceil (length / 2 );} // find again the length of the intermediate node = Node head;! The while (length ==. 1 ) = {Node Node. Next; length-- ;} return Node;};

 

Find a single list penultimate node K

// ideas 
simple idea: cycle to the end of the list to find the length find Length- k nodes need to loop twice. 

@ Optimization: 
setting two nodes, a difference of distance k nodes, the current node reaches the end surface, the back of the access node. 
After reaching k, the nodes behind the previous node before departure. 
// Code robustness: for the head to be considered null, k is 0, k is larger than the length of the list. 
function FindKthToTail (head, K) { 
      IF return null (K head ||!!) ;  the let = Front head;  the let behind = head; index = the let. 1 ; the while (front.next) {index ++ ; Front = front.next; IF (index> K) = {behind behind.next;}} return (K <= index) && behind;}




 

Singly linked list reversal

// list to the head node as a reference node 

// the next node is moved to the head of the reference node as the primary node 

// When the next reference node is null, then it has become the last node has been inverted to complete list 

 reverseList function = var (head) { 
      the let the currentNode = null ; 
      the let HeadNode = head; 
      the while (head && head.next) {  the currentNode = head.next;  head.next = currentNode.next; currentNode.next = HeadNode; HeadNode = the currentNode ;} return HeadNode;};

 Array to a linked list

function array2list (ary) { 
    IF (! ary.length) { 
        return null 
    } 

    var Node  var head = {value: ary [0], Next: null }  var = pNode head pNode // variables for a node before storage 
 for ( I. 1 = var; I <ary.length; I ++ ) {value = {node: ary [I], next: null } pnode.next = node // to point to the current node pnode next previous node = node // will assigned to node pNode } return head}

 

Chain transfer array

function list2array(head) {
    if(!head) {
        return []
    }
    var result = [head.value]
    var restValues = list2array(head.next)
    return result.concat(restValues) }

Parity list

// Title: to order list, combines all odd nodes, then the node is an even number. 
// idea 

special case, the empty / single / double-linked list not need to modify the order of 
odd odd node point, point to the even an even number of nodes, evenHead even save the first node 
after the control loop while shifting conditions: the even && && odd even.next, because even.next even need exists, so the first even determining, 
since odd.next caught in the middle, so only need to determine the final amount present even.next odd even before, the first mobile odd-- .next first change pointer, then ODD
/ .next points to the location of the even. Finally parity connection list, the head node returns the head var oddEvenList = function (head) { IF (head.next head || ||!!! {Head.next.next) return head; } the let ODD = head, // ODD point odd node evenHead = head.next, the evenHead.next =; // the even points even number of nodes, evenHead save the first node coupling the while (ODD the even && && even.next) { odd.next = even.next; // odd odd node pointing to node odd = odd.next ; // the odd odd pointer to a node even.next = odd.next; // coupling node points to an even node even = even.next; // at even a pointer to a node odd } odd.next = evenHead; / / connection list parity return head; }; 

Merging two single list

@ Ideas:
 
two linked lists, each node pointing cursor is provided a head node, the value of the cursor on the comparison node, 


next node is equal to the smaller value of the small and large next node node to another. Small values that put out to merge the list, 

so recursion. 

Return small node. 

// consider the robustness of the code, the termination condition is recursive, is null, the two head, the other nodes take to return. 
function the Merge (pHead1, pHead2) { 
      IF (! pHead1) { 
        return pHead2;  }  IF (! pHead2) {  return pHead1;} the let head; IF (pHead1.val < pHead2.val) = {head pHead1; head.next = the Merge (pHead1.next, pHead2);} the else {head = pHead2; = head.next the Merge (pHead1, pHead2.next);} return head;}

 

The first two lists of common node

// ideas 
1 . Length1 two first find the length of the list, length2 

2. let go a little bit longer list of length2- length1 step, so that long list and short list the same starting point 

3 . Move forward together two lists, compare get the first node equal 

time complexity of O (length1 + length2) space complexity O (0 ) 
 function FindFirstCommonNode (pHead1, pHead2) {  IF {return null (pHead1 pHead2 ||!!) ;} // Get list length1 = length of the let getLength (pHead1); the let length2 = getLength (pHead2); // long chain lang preceding the let, Short , interval The; IF (length1> length2) {lang = pHead1; = Short pHead2; interval The length1 = - length2;} the else {lang = pHead2; = Short pHead1; interval The length2 = - length1;} the while (interval-- ) = {lang lang.next; } // 找相同节点 while (lang) { if (lang === short) { return lang; } lang = lang.next; short = short.next; } return null; } function getLength(head) { let current = head; let result = 0; while (current) { result++; current = current.next; } return result; }

 

Doubly linked list

// 链表节点
class Node {
    constructor(element) {
        this.element = element
        this.prev = null
        this.next = null
    }
}

// 双向链表
class DoublyLinkedList {

    constructor() {
        this.head = null
        this.tail = null
        this.length = 0
    }

    // 任意位置插入元素
 insert(position, element) { if (position >= 0 && position <= this.length){ const node = new Node(element) let current = this.head let previous = null let index = 0 // 首位 if (position === 0) { if (!head){ this.head = node this.tail = node } else { node.next = current this.head = node current.prev = node } // 末位 } else if (position === this.length) { current = this.tail current.next = node node.prev = current this.tail = node // 中位 } else { while (index++ < position) { previous = current current = current.next } node.next = current previous.next = node current.prev = node node.prev = previous } this.length++ return true } return false } // 移除指定位置元素  removeAt(position) { if (position > -1 && position < this.length) { let current = this.head let previous = null let index = 0 // 首位 if (position === 0) { this.head = this.head.next this.head.prev = null if (this.length === 1) { this.tail = null } // 末位 } else if (position === this.length - 1) { this.tail = this.tail.prev this.tail.next = null // 中位 } else { while (index++ < position) { previous = current current = current.next } previous.next = current.next current.next.prev = previous } this.length-- return current.element } else { return null } } // 其他方法... }

 

Guess you like

Origin www.cnblogs.com/huahongcui/p/11520845.html