class ListNode {
constructor(val) {
this.val = val;
this.next = null;
}
}
let node1 = new ListNode(1);
let node2 = new ListNode(2);
let node3 = new ListNode(3);
let node4 = new ListNode(4);
let node5 = new ListNode(5);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
const mergeTwoLists = function(l1, l2) {
let head = new ListNode()
let cur = head
while(l1 && l2) {
if(l1.val<=l2.val) {
cur.next = l1
l1 = l1.next
} else {
cur.next = l2
l2 = l2.next
}
cur = cur.next
}
cur.next = l1!==null?l1:l2
return head.next
};
const deleteDuplicates = (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;
}
const deleteRepeat = (head) => {
if (!head || !head.next) {
return false;
}
const dummy = new ListNode();
dummy.next = head;
let cur = dummy;
while (cur.next && cur.next.next) {
if (cur.next.val === cur.next.next.val) {
let sameVal = cur.next.val;
while (cur.next && cur.next.val === sameVal) {
cur.next = cur.next.next;
}
} else {
cur = cur.next
}
}
return dummy.next;
};
deleteRepeat(node1);
const deleteIndex = (head, n) => {
if (!head || !head.next) {
return false;
}
const dummy = new ListNode();
dummy.next = head;
let fast = dummy;
let slow = dummy;
while(n !== 0) {
fast = fast.next;
n --;
}
while (fast.next) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummy.next;
};
const reverseList = (head) => {
let pre = null;
let cur = head;
while (cur !== null) {
let next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
reverseList(node1);
console.log('444', node1);
console