Leetcode 237 Delete node in a Linked list (delete nodes in linked list)

Source: Internet
Author: User

translation
给定一个访问节点的路径,写一个函数去删除在一个单向链表中除尾部以外的节点。假设这个链表是1234,并且你被给予了第3个值为3的节点,那么在调用你的函数之后这个链表应该变为124。
Original
Write a function toDelete a node (except theTailinchA singly linkedList,givenOnly access to  thatNode. Supposed theLinkedList  is 1-2-3-4  andYou aregiven  the ThirdNode withValue3, theLinkedListShould become1-2-4  AfterCalling your function.
Code
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/class Solution {public:    voiddeleteNode(ListNode* node) {        if (node == NULL) {}        else {            ListNode* tmp = node->next;            node->val = tmp->val;            node->next = tmp->next;            delete tmp;        }    }};

Leetcode 237 Delete node in a Linked list (delete nodes in linked list)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.