{"id":192,"date":"2018-06-04T19:25:08","date_gmt":"2018-06-04T11:25:08","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=192"},"modified":"2018-11-27T17:25:51","modified_gmt":"2018-11-27T09:25:51","slug":"237-delete-node-in-a-linked-list","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=192","title":{"rendered":"237. Delete Node in a Linked List"},"content":{"rendered":"<p>Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.<\/p>\n<p>Given linked list &#8212;\u00a0head =\u00a0[4,5,1,9], which looks like following:<\/p>\n<pre>    4 -&gt; 5 -&gt; 1 -&gt; 9\r\n<\/pre>\n<p><strong>Example 1:<\/strong><\/p>\n<pre><strong>Input:<\/strong> head = [4,5,1,9], node = 5\r\n<strong>Output:<\/strong> [4,1,9]\r\n<strong>Explanation: <\/strong>You are given the second node with value 5, the linked list\r\n\u00a0            should become 4 -&gt; 1 -&gt; 9 after calling your function.\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre><strong>Input:<\/strong> head = [4,5,1,9], node = 1\r\n<strong>Output:<\/strong> [4,5,9]\r\n<strong>Explanation: <\/strong>You are given the third node with value 1, the linked list\r\n             should become 4 -&gt; 5 -&gt; 9 after calling your function.\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ul>\n<li>The linked list will have at least two elements.<\/li>\n<li>All of the nodes&#8217; values will be unique.<\/li>\n<li>The given node\u00a0will not be the tail and it will always be a valid node of the linked list.<\/li>\n<li>Do not return anything from your function.<\/li>\n<\/ul>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\n# Definition for singly-linked list.\r\n# class ListNode(object):\r\n#     def __init__(self, x):\r\n#         self.val = x\r\n#         self.next = None\r\n\r\nclass Solution(object):\r\n    def deleteNode(self, node):\r\n        &quot;&quot;&quot;\r\n        :type node: ListNode\r\n        :rtype: void Do not return anything, modify node in-place instead.\r\n        &quot;&quot;&quot;\r\n        node.val = node.next.val\r\n        node.next = node.next.next\r\n        \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Write a function to delete a node (except the tail) in <a href=\"https:\/\/www.ccagml.com\/?p=192\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">237. Delete Node in a Linked List<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/192"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=192"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":193,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/192\/revisions\/193"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}