CCI2.3 deletes a node in a one-way linked list. Assume that you can only access this node.

Source: Internet
Author: User
Implement an algorithm to delete a node in the middle of a one-way linked list. Suppose you can only access this node. Example: input: node c IN a-B-c-d-e of the one-way linked list: No data is returned, but the result of this linked list is a-B-d-epackagetest; publicclassRemoveNode {because only the current node can be accessed, there is no way to get reference from the previous Node

Implement an algorithm to delete a node in the middle of a one-way linked list. Suppose you can only access this node. Example: input: node c IN a-B-c-d-e of the one-way linked list: No data is returned, but the linked list is changed to a-B-d-e package test; public class RemoveNode {// because only the current node can be accessed, there is no way to get reference from the previous Node

Implement an algorithm to delete a node in the middle of a one-way linked list. Suppose you can only access this node.

Example:

Input: node c in one-way linked list a-> B-> c-> d-> e

Result: No data is returned, but the linked list is a-> B-> d-> e.

Package test; public class RemoveNode {// because only the current node can be accessed, there is no way to get the reference of the previous node, so you can only use the method of exchange value, // assign the value of the subsequent Node to the current node, and then delete the deleted node public void removeNode (node Node) {if (node = null) return; if (node. next! = Null) {node. val = node. next. val; node. next = node. next. next;} else {node = null ;}}}

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.