LCR-136-删除链表结点

Last updated on December 18, 2023 am

删除链表结点的两种解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head == null) return null;
if(head.val == val) return head.next;
else head.next = deleteNode(head.next,val);
return head;

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode pre = head;
ListNode cur = head.next;

if (head.val == val) return head.next;

while (cur != null) {
if (cur.val == val) {
pre.next = cur.next;
}
pre = cur;
cur = cur.next;

}
return head;
}
}

LCR-136-删除链表结点
https://wlei224.gitee.io/2023/10/16/LCR-136-删除链表结点/
Author
WLei224
Posted on
October 16, 2023
Updated on
December 18, 2023
Licensed under