Sword refers to Java Implementation of offer programming questions -- interview question 13 deletes linked list nodes in O (1) time, and sword refers to offer

Source: Internet
Author: User

Sword refers to Java Implementation of offer programming questions -- interview question 13 deletes linked list nodes in O (1) time, and sword refers to offer

Question: given a one-way linked list head pointer and a node pointer, define a function to delete the node at O (1) time.

Given a one-way linked list, the time complexity of normal Chain List deletion is the time complexity of searching the linked list, that is, O (n). If you need to delete a node within the O (1) time complexity, the method of finding the previous node and next node of the node through the traversal chain table does not work. Therefore, the idea is that, based on the given node to be deleted, you can directly find the node in the next year and copy the content of the subsequent node to the current node, at the same time, direct the current node to the back of the node to ensure that the linked list is continuously opened. Deleting the next node is equivalent to deleting the given node.

One thing to consider is that if the node to be deleted is the end node of the linked list, you still need to traverse from the first node to the previous node of the End Node in order, and then delete the end node, the average time complexity is [(n-1) * 1 + O (n)]/n, and the result is still O (1 ).

 

1/** 2 * offoffoffoffer interview question 13: delete a linked list node at O (1) time 3 * question: given a one-way linked list head pointer and a node pointer, define a function to delete the node at O (1) time. 4 * @ author GL 5*6 */7 public class No13DeleteNodeInList {8 9 public static class ListNode {10 public int data; 11 public ListNode next; 12 public ListNode (int data, listNode next) {13 this. data = data; 14 this. next = next; 15} 16} 17 18 public static void deleteNode (ListNode head, ListNode node) {19 // Delete the last node, locate the previous node 20 if (node. next = null) {21 while (head. next! = Node) {22 head = head. next; 23} 24 head. next = null; 25} 26 // The node to be deleted is the header node 27 else if (head = node) {28 head = null; 29} 30 // The node to be deleted is a common intermediate node 31 else {32 node. data = node. next. data; 33 node. next = node. next. next; 34} 35} 36 public static void main (String [] args) {37 ListNode tail = new ListNode (1, null); 38 ListNode c = new ListNode (2, tail); 39 ListNode B = new ListNode (3, c); 40 ListNode head = new ListNode (4, B); 41 deleteNode (head, c); 42 while (head! = Null) {43 System. out. println (head. data); 44 head = head. next; 45} 46 47} 48}

 

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.