Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to delete the nodes of the leetcode linked list

Shulou Source: shulou.com Published: 2022-06-01 13:11:52 10月02日 Update

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!

Tags: Nodes functions median content more examples articles topics contingency interpretation input output good practical same three provenance one-way commercial official Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Information Shulou Technology MariaDB MySQL