Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete the nodes of the leetcode linked list

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to delete the nodes of the leetcode linked list. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The title gives the header pointer of the one-way linked list and the value of a node to be deleted, and defines a function to delete the node. Returns the header node of the deleted linked list. Note: this question compared with the original title has been changed example 1: input: head = [4Magne 5dje 1je 9], val = 5 output: [4je 1jue 9] explanation: given your linked list value of 5 of the second node, then after calling your function, the linked list should become 4-> 1-> 9. Example 2: input: head = [4Magne 5dint 1je 9], val = 1 output: [4Magne 5je 9] explanation: given the third node in your linked list with a value of 1, then after calling your function, the linked list should become 4-> 5-> 9. Description: title ensures that the values of nodes in the linked list are different if you use C or C++ language, you do not need free or delete deleted node Source: LeetCode link: https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. Answer the question / 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) {ListNode cursor = head; ListNode preNode = null; if (cursor.val = = val) {return head.next } while (cursor.val! = val) {preNode = cursor; cursor = cursor.next;} preNode.next = preNode.next.next; return head;}} Thank you for reading! On "leetcode linked list how to delete linked list nodes" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report