Leetcode 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 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.


解题思路:

链表基本操作,记待删除节点为node

令node.val = node.next.val,node.next = node.next.next即可


Java code:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9  public void deleteNode(ListNode node) {
10       node.val =   node.next.val;
11       node.next = node.next.next;
12  }

Reference:

1. http://bookshadow.com/weblog/2015/07/15/leetcode-delete-node-linked-list/

 

posted @ 2015-09-08 10:07  茜茜的技术空间  阅读(134)  评论(0编辑  收藏  举报