总结 对链表的操作主要分为两种 : 1. 多个指针配合操作节点,一般空间复杂度低; 2. 递归操作; 3. 还可以使用java内置的数据结构,比如 等等; 其他要点 : 1. 对一个位置的删除和插入,都需要知道前一个节点; 1. 虚头部用于保留“前一个节点”; 2. 节点赋值可以转移“
83. Remove Duplicates from Sorted List:https://leetcode.com/problems/remove-duplicates-from-sorted-list/ 删除重复元素:(判空;dummy;while(head.next!=null)-val等则删,否则head移动;) 我的解法:(Test case [1,1,1]--point: 重复次数大于2, next=null; 首先要判断head.next != null; 然后while(he...
题目链接https://leetcode.com/problems/intersection-of-two-linked-lists/ 题意很好懂,问题在于如何找到相交的node,想到的最简单的方法从node的最后一个节点去往前数,遇到分叉,则返回当前节点,否则返回None。这里用 两个list去保存node,从list的最后一个位置开始往前进。 代码如下,提交后,超过 99.84% 提交代码的...
Copy List with Random Pointer A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 题目本身不难,但是做了几遍才做对。注意已经处理过的点(map contains)不要再往queue里加了。 1...
Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3->4Output: 1->1->2->3->4->4 A Use two-pointer. One on l1. one on l2. Let the smaller...
Collection of various algorithms in mathematics, machine learning, computer science, physics, etc implemented in C for educational purposes. - C/data_structures/linked_list/middle_element_in_list.c at e5dad3fa8def3726ec850ca66a7f51521f8ad393 · TheAlgori
Find the middle point of the list and divide the list into two parts by setting mid.next = null; reverse the second half Merge two list by retrieving one node from each list at a time Code public class Solution { public void reorderList(ListNode head) { ...
Here's a list of 30 coding interview patterns, each with one or two example LeetCode problems:1. Sliding WindowProblem 1: Minimum Size Subarray Sum Problem 2: Longest Substring Without Repeating Characters2. Two PointersProblem 1: 3Sum Problem 2: Container With Most Water...
point1.next=point1.next.next; return dummy.next; } } 83. Remove Duplicates from Sorted List 有可能删除head的时候需要dummy.next=head,否则只需要dummy=head就可以了。 注意三个连续重复的现象。 class Solution { public ListNode deleteDuplicates(ListNode head) { ...
LeetCode 142. Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to...