码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode OJ 237】Delete Node in a Linked List

时间:2016-01-21 13:57:03      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:https://leetcode.com/problems/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.

解题思路:删除单链表中指定的节点,一般有两种方法:第一种是遍历得到指定节点的前一个节点,将该节点的next执行指定节点的下一个节点,然后删除指定节点,第二个方法是将指定节点下一个节点的data值赋给指定节点,然后将指定节点的next指向该节点的下下一个节点即可。本题显然可以使用第二种方法来解决。

示例代码:

public class Solution
{
	class ListNode
	{
		int val;
		ListNode next;
		ListNode(int x)
		{
			val = x;
		}
	}
	public void deleteNode(ListNode node)
	{
		if(node==null)
			return ;
		node.val=node.next.val;
		node.next=node.next.next;	
	}
}


【LeetCode OJ 237】Delete Node in a Linked List

标签:

原文地址:http://blog.csdn.net/xujian_2014/article/details/50548498

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