Leetcode 237. Delete Node in a Linked list linked list

Source: Internet
Author: User

237. Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

supposed the linked list Is 1, 4  and You is given the third node with Value 3 , the linked list should become 1, 2, 4 after calling your function.

Main topic:

Given a node in a single-linked list, delete the node.

Ideas:

Because you cannot know the previous node of this node, you can exchange information from the node that is currently being deleted to the next node of the node. Then delete the next node. This allows the node to be deleted.

/** * definition for singly-linked list. * struct listnode {  *     int val; *     ListNode *next;  *     listnode (int x)  : val (x),  next (NULL)  {} *  }; */class solution {public:    void deletenode (ListNode*  node)  {        if (Null == node)              return ;         Listnode * next = node->next;        node-> val = next->val;        node->next = next- >next;        delete next;    }};

The topic is not very understood.

2016-08-12 21:05:17

This article is from the "Do Your best" blog, so be sure to keep this source http://qiaopeng688.blog.51cto.com/3572484/1837436

Leetcode 237. Delete Node in a Linked list 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.