[CrackCode] 2.3 Delete a node in the middle of a single linked list
2014-01-27 01:44
435 查看
Implement an algorithm to delete a node in the middle of a single linked list, givenonly access to that node
EXAMPLE
Input: the node ‘c’ from the linked list a->b->c->d->e
Result: nothing is returned, but the new linked list looks like a->b->d->e
=================
Analysis:
We can do nothing to delete the target as we do not have the target node's previous node. However, we can copy all the attribute of the node after the target node (say "next node") to target node, then delete the "next node". Result of which is similar
to deleting the current node.
Node that if target node is the last node of the linked list, this approach would not work.
Note: Please download the crackcode library before running the program.
EXAMPLE
Input: the node ‘c’ from the linked list a->b->c->d->e
Result: nothing is returned, but the new linked list looks like a->b->d->e
=================
Analysis:
We can do nothing to delete the target as we do not have the target node's previous node. However, we can copy all the attribute of the node after the target node (say "next node") to target node, then delete the "next node". Result of which is similar
to deleting the current node.
Node that if target node is the last node of the linked list, this approach would not work.
public class Answer { public static void solution(LinkedListNode target){ if(target == null || target.next == null) return; LinkedListNode next = target.next; target.data = next.data; target.next = next.next; return; } public static void main(String[] args) { LinkedListNode head = AssortedMethods.randomLinkedList(10, 0, 10); System.out.println(head.printForward()); solution(head.next.next.next); // delete node 4 System.out.println(head.printForward()); } }
Note: Please download the crackcode library before running the program.
相关文章推荐
- 【11】Delete a node in the middle of a single linked list
- Q.2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only acce
- LintCode 372: Delete Node in the Middle of Singly Linked List
- 372. 在O(1)时间复杂度删除链表节点 (delete-node-in-the-middle-of-singly-linked-list)(c++)----lintcode面试题之链表
- Lintcode372 Delete Node in the Middle of Singly Linked List solution 题解
- lintcode 容易题:Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- Delete Node in the Middle of Singly Linked List
- LintCode 372. Delete Node in the Middle of Singly Linked List
- LintCode Delete Node in the Middle of Singly Linked List 在O(1)时间复杂度删除链表节点
- lintcode-easy-Delete Node in the Middle of Singly Linked List
- #372 Delete Node in the Middle of Singly Linked List
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
- LintCode: Delete Node in the Middle of Singly Linked List
- LinkedList---Delete Node in the Middle of Singly Linked List
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
- Ch2-3: remove the middle node in a singly linked list
- PIQ18: Delete middle node of a linked list
- fwnx - find the middle node in the linked list ~~~
- Delete Node in a Linked List
- Delete Node in a Linked List