题目:
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
注意:此题对比原题有改动
示例 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 被删除的节点
思路一:
使用双指针
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val) return head.next;
ListNode pre = head, cur = head.next;
while(cur != null && cur.val != val) {
pre = cur;
cur = cur.next;
}
if(cur != null) pre.next = cur.next;
return head;
}
}
思路二:
使用单指针进行遍历
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val) return head.next;
ListNode pre = head;
while(pre.next!= null && pre.next.val != val) {
pre = pre.next;
}
if(pre.next != null) pre.next = pre.next.next;
return head;
}
}
思路三:
创建一个假头结点
而不用单独分类讨论 直接一并使用
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode temp = new ListNode(0);
temp.next = head;
ListNode cur=temp;
while(cur.next!=null){
if(cur.next.val==val){
cur.next = cur.next.next;
return temp.next;
}
cur = cur.next;
}
return temp.next;
}
}