码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode237_Delete Node in a Linked List(删除链表中的节点) Java题解

时间:2015-07-29 10:22:45      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:deletenodes   链表   java   leetcode   

题目:

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.

解题:

今天早上无意间看到链表里还有一题easy的没有做,果断做了,看到题目之后,感觉这题有点像脑筋急转弯,它要求删除链表中的一个节点,而且只给那个节点,不给其他信息,另外还说了这个节点不会是最后一个节点。

解题思路就是,用节点的下一个节点的值覆盖要删除的那个节点,然后删除下一个节点,就这样。

代码:

public void deleteNode(ListNode node) {
		node.val=node.next.val;
		node.next=node.next.next;
        
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode237_Delete Node in a Linked List(删除链表中的节点) Java题解

标签:deletenodes   链表   java   leetcode   

原文地址:http://blog.csdn.net/u012249528/article/details/47121767

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!