LeetCode237: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.

 

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void deleteNode(ListNode node) {
        if(node==null)
            return;
        if(node.next==null)
            node=null;
        node.val=node.next.val;
        node.next=node.next.next;
    }
}




阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shangqing1123/article/details/49890295
文章标签: leetcode LinkedList
个人分类: LeetCode
上一篇韩顺平Linux笔记(十六)——压缩和解压
下一篇LeetCode203:Remove Linked List Elements
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭