Description
Implement an algorithm to delete a node in the middle of a singly linked list, and given only access to that node.
Example
Linked list is 1->2->3->4
, and given node 3
, delete the node1->2->4
Solve the problem: delete the specified node. Because Java does not have delete or free, if the node to be deleted is the last, it cannot be deleted without knowing the head node. The code is as follows:
/*** Definition for ListNode. * public class ListNode {* int val; * ListNode Next; * ListNode (int val) {* This.val = val; * This.next = null; * } * } */ Public classSolution {/** @param node:the node in the list should is Deletedt * @return: Nothing*/ Public voidDeletenode (ListNode node) {//Write your code here if(node = =NULL|| Node.next = =NULL) return; Node.val=Node.next.val; Node.next=Node.next.next; return; }}
372. Delete Node in a Linked List "Lintcode java"