- 删除链表中的节点
- 这题什么鬼啊,第二个参数在哪输入的啊????
ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
/**
Do not return anything, modify it in-place instead.
*/
function deleteNode(root: ListNode | null): void {
root.val = root.next.val
root.next = root.next.next
};
py
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val = node.next.val
node.next = node.next.next
- 删除链表的倒数第 N 个结点
ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
const getLen = (node: ListNode, len: number): number => {
if (node?.next) {
return getLen(node.next, len + 1)
} else {
return len
}
}
const len: number = getLen(head, 0)
const jian = len - n
if (jian == -1) {
head = head.next
return head
}
const func = (node: ListNode, num: number) => {
if (num === jian) {
if (node?.next?.next) {
node.next = node.next.next
} else {
node.next = null
}
return
} else {
return func(node.next, num + 1)
}
}
func(head, 0)
return head
};
py
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
len = 0
node1 = head
while(node1.next!=None):
node1 = node1.next
len+=1
jian = len - n
if jian == -1:
return head.next
num = 0
node1 = head
while num<=jian:
if num == jian:
node1.next = node1.next.next
else:
node1 = node1.next
num+=1
return head
- 反转链表
ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode = null
let curr: ListNode = head
while (curr) {
const next = curr.next
curr.next = prev
prev = curr
curr = next
}
return prev
}
- 合并两个有序链表
ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
if (list1 === null) {
return list2;
} else if (list2 === null) {
return list1;
} else if (list1.val < list2.val) {
list1.next = mergeTwoLists(list1.next, list2);
return list1;
} else {
list2.next = mergeTwoLists(list1, list2.next);
return list2;
}
};
- 回文链表
ts
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function isPalindrome(head: ListNode | null): boolean {
if (head.val === null) {
return true
}
const getList = (arr: number[], node: ListNode | null): number[] => {
if (node && node.val !== null) {
arr.push(node.val)
return getList(arr, node.next)
} else {
return arr
}
}
const list = getList([], head)
const len = list.length
for (let i = 0; i < Math.floor(len / 2); i++) {
if (list[i] !== list[len - i - 1]) {
return false
}
}
return true
};
- 环形链表
ts 双指针
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function hasCycle(head: ListNode | null): boolean {
let fast: ListNode = head
let slow: ListNode = head
if (!head || !head.next) return false
while (fast && fast.next) {
fast = fast.next.next
slow = slow.next
if (slow === fast) {
return true
}
}
return false
};
ts 经过标记
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function hasCycle(head: ListNode | null): boolean {
let node = head
while (node && node.next) {
if (node.val === null) {
return true
}
node.val = null
node = node.next
}
return false
};