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; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/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...
阅读 3028·2021-11-22 09:34
阅读 2505·2021-09-30 09:47
阅读 1438·2021-09-03 10:32
阅读 3702·2021-08-16 10:49
阅读 1784·2019-08-30 15:55
阅读 2451·2019-08-30 15:52
阅读 3316·2019-08-30 15:44
阅读 1343·2019-08-30 15:44