510 字
3 分钟
删除链表的节点
本题较为简单,熟悉链表应该达到随手就能解出的程度
题目
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
注意:此题对比原题有改动
示例 1:
输入: head = [4,5,1,9], val = 5输出: [4,1,9]解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.示例 2:
输入: head = [4,5,1,9], val = 1输出: [4,5,9]解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.说明:
题目保证链表中节点的值互不相同 若使用 C 或 C++ 语言,你不需要 free 或 delete 被删除的节点
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
一、 思路
本题意在移除一个节点,即:
1 -> 2 -> 3 -> 4 -> 5
如果要移除3节点,就要将2节点的next指向4。所以在遍历链表的时候我们在找到移除目标的时候,需要直到上个节点是什么。
这里引入一个指针 lastNode 保存上个节点,遍历时 currentNode 作为指向当前节点的指针。
遇到目标节点后,将当前节点的 next 赋值给 lastNode 的 next,返回 head。
二、 双指针解法

func deleteNode(_ head: ListNode?, _ val: Int) -> ListNode? { var lastNode: ListNode? var currentNode = head
while let node = currentNode { if node.val == val { if let last = lastNode { last.next = node.next return head } else { return node.next } }
lastNode = node currentNode = node.next }
return head}三、 单指针解法
func deleteNode2(_ head: ListNode?, _ val: Int) -> ListNode? { if head?.val == val { return head?.next } var currentNode = head while let node = currentNode?.next { if node.val == val { currentNode?.next = node.next return head } else { currentNode = currentNode?.next } } return head}