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; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/65536.html
摘要:遞歸法復(fù)雜度時(shí)間空間思路當(dāng)遞歸到鏈表尾部時(shí)返回,每次返回時(shí)長(zhǎng)度加,一旦長(zhǎng)度為時(shí)記錄下該節(jié)點(diǎn)。雙指針法復(fù)雜度時(shí)間空間思路用兩個(gè)指針,快指針先走步,然后快慢指針同時(shí)開始走,保持的距離,這樣當(dāng)快指針到達(dá)末尾時(shí),慢指針就是倒數(shù)第個(gè)。 Nth to Last Node in List Find the nth to last element of a singly linked list. ...
摘要:依然是一道找倒數(shù)第個(gè)結(jié)點(diǎn)的鏈表題,用雙指針做。先走,然后和一起走,直到為,的位置就是倒數(shù)第個(gè)位置。 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...
閱讀 3030·2021-11-22 09:34
閱讀 2506·2021-09-30 09:47
閱讀 1439·2021-09-03 10:32
閱讀 3704·2021-08-16 10:49
閱讀 1784·2019-08-30 15:55
閱讀 2451·2019-08-30 15:52
閱讀 3316·2019-08-30 15:44
閱讀 1344·2019-08-30 15:44