LeetCode(剑指 Offer)- 18. 删除链表的节点
Posted 放羊的牧码
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode(剑指 Offer)- 18. 删除链表的节点相关的知识,希望对你有一定的参考价值。
题目链接:点击打开链接
题目大意:略
解题思路:略
相关企业
- 亚马逊(中国)投资有限公司
AC 代码
- Java
/**
* Definition for singly-linked list.
* public class ListNode
* int val;
* ListNode next;
* ListNode(int x) val = x;
*
*/
// 解决方案(1)
class Solution
public ListNode deleteNode(ListNode head, int val)
ListNode tmp = head, pre = head;
if (head != null && head.val == val)
return head.next;
while (tmp != null)
if (tmp.val != val)
pre = tmp;
tmp = tmp.next;
continue;
pre.next = tmp.next;
break;
return head;
// 解决方案(2)
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;
- C++
/**
* Definition for singly-linked list.
* struct ListNode
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL)
* ;
*/
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;
;
以上是关于LeetCode(剑指 Offer)- 18. 删除链表的节点的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode(剑指 Offer)- 18. 删除链表的节点
LeetCode(剑指 Offer)- 18. 删除链表的节点
LeetCode Algorithm 剑指 Offer 18. 删除链表的节点