
删除链表的节点
C++代码
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val){
if(head->val == val)
return head->next;
ListNode* pre = head, *cur = head->next;
while(cur != nullptr && cur->val != val){
pre = cur;
cur = cur->next;
}
if(cur != nullptr)
pre->next = cur->next;
return head;
}
};
Java代码
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;
}
}
联系方式
如果有任何问题可以邮箱联系我:raymondlam1@yeah.net
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)