Problem
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
Note就是把node.next.val賦給node,然后刪掉node.next,用node直接連接node.next.next。
Solutionpublic class Solution { public void deleteNode(ListNode node) { if (node == null) return; if (node.next != null) { node.val = node.next.val; node.next = node.next.next; } return; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/65536.html
摘要:遞歸法復雜度時間空間思路當遞歸到鏈表尾部時返回,每次返回時長度加,一旦長度為時記錄下該節點。雙指針法復雜度時間空間思路用兩個指針,快指針先走步,然后快慢指針同時開始走,保持的距離,這樣當快指針到達末尾時,慢指針就是倒數第個。 Nth to Last Node in List Find the nth to last element of a singly linked list. ...
摘要:依然是一道找倒數第個結點的鏈表題,用雙指針做。先走,然后和一起走,直到為,的位置就是倒數第個位置。 Problem Find the nth to last element of a singly linked list. The minimum number of nodes in list is n. Example Given a List 3->2->1->5->null ...
Problem You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1s digit is at the head of the list. Write a f...
Problem Write a program to find the node at which the intersection of two singly linked lists begins. Example The following two linked lists: A: a1 → a2 ↘ ...
Problem Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. Example Example:Given...
閱讀 3045·2021-11-22 09:34
閱讀 2516·2021-09-30 09:47
閱讀 1448·2021-09-03 10:32
閱讀 3720·2021-08-16 10:49
閱讀 1794·2019-08-30 15:55
閱讀 2465·2019-08-30 15:52
閱讀 3325·2019-08-30 15:44
閱讀 1359·2019-08-30 15:44