链表

链表中倒数第k个结点

旧巷老猫 提交于 2020-03-23 02:43:54
题目:输入一个链表,输出该链表中倒数第k个结点。 给定的链表节点: class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } } 一开始我的想法是,先定义一个长度为k的ListNode数组,然后遍历一遍链表的所有节点,并把每个节点循环存到这个数组里面。“循环存”就是当数组存满的时候,返回到数组第0个位置开始覆盖存储,最后从倒数第一个逆推到倒数第k个。 代码如下: public ListNode FindKthToTail(ListNode head,int k) { ListNode[] listNodes = new ListNode[k]; int index = -1; int count = 0; if (head == null || k == 0) return null; while (head != null) { index = (index + 1) % k; listNodes[index] = head; head = head.next; count++; } if (k > count) return null; //k大于链表结点的个数时 //从倒数第一个往回推 count = 1; while (count < k) { index =

【LeetCode】链表 linked list(共34题)

[亡魂溺海] 提交于 2020-03-22 22:41:17
/*--> */ /*--> */ 【2】Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序。 Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 12 if (!l1 || !l2) { 13 return l1 == nullptr ? l2 : l1; 14 } 15 ListNode *h1 = l1, *h2 = l2; 16 ListNode *head = 0, *tail = 0; 17 int carry = 0; 18 while (h1 &&

leetcode 382. 链表随机节点(Linked List Random Node)

旧街凉风 提交于 2020-03-22 22:17:29
目录 题目描述: 示例: 解法: 题目描述: 给定一个单链表,随机选择链表的一个节点,并返回相应的节点值。保证每个节点 被选的概率一样 。 进阶: 如果链表十分大且长度未知,如何解决这个问题?你能否使用常数级空间复杂度实现? 示例: // 初始化一个单链表 [1,2,3]. ListNode head = new ListNode(1); head.next = new ListNode(2); head.next.next = new ListNode(3); Solution solution = new Solution(head); // getRandom()方法应随机返回1,2,3中的一个,保证每个元素被返回的概率相等。 solution.getRandom(); 解法: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: /** @param head The linked list's head. Note that the head is guaranteed to be not null,

[leetCode][003] Intersection of Two Linked Lists

无人久伴 提交于 2020-03-22 22:04:17
【题目】:     Write a program to find the node at which the intersection of two singly linked lists begins.     For example, the following two linked lists:       A: a1 → a2             ↘              c1 → c2 → c3            ↗    B: b1 → b2 → b3    begin to intersect at node c1.   Notes:     1. If the two linked lists have no intersection at all, return null.     2. The linked lists must retain their original structure after the function returns.     3. You may assume there are no cycles anywhere in the entire linked structure.     4. Your code should preferably run in O(n) time and use only O(1)

leetcode 160. 相交链表(Intersection of Two Linked Lists)

喜夏-厌秋 提交于 2020-03-22 22:02:32
目录 题目描述: 示例 1: 示例 2: 示例 3: 注意: 解法: 题目描述: 编写一个程序,找到两个单链表相交的起始节点。 如下面的两个链表: 在节点 c1 开始相交。 示例 1: ``` 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 输出:Reference of the node with value = 8 输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。 从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。 在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。 ``` 示例 2: ``` 输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 输出:Reference of the node with value = 2 输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。 从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。 在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1

LeetCode刷题之合并排序链表

╄→尐↘猪︶ㄣ 提交于 2020-03-22 21:54:02
合并两个有序链表并返回一个新的列表。新列表应该由连接在一起的节点前两个列表 给定实例: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 思路分析: 引入第三个链表,存储合并之后的链表,开两个指针,分别遍历两个链表,当遍历到一个节点的时候,就开始判断大小,然后将小的链表节点存储到第三个链表中,依次递归判断。但是我们需要考虑临界条件,如果第一个链表的数都比第二个链表的小,那么我们就直接将第二个链表链接到第三个链表的next域中就行。 代码如下: class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def mergeTwoLists(self, l1, l2): if l1 is None: return l2 if l2 is None: return l1 pMerge = ListNode(None) if l1.val < l2.val: pMerge = l1 pMerge.next = self.mergeTwoLists(l1.next, l2) else: pMerge = l2 pMerge.next = self.mergeTwoLists(l1, l2.next) return pMerge

206. 反转链表

不羁的心 提交于 2020-03-22 18:10:41
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution 10 { 11 public: 12 ListNode* reverseList(ListNode* head) 13 { 14 ListNode* new_head = NULL; 15 while(head) 16 { 17 ListNode* temp = head->next; 18 head->next = new_head; 19 new_head = head; 20 head = temp; 21 } 22 return new_head; 23 } 24 }; 来源: https://www.cnblogs.com/yuhong1103/p/12547346.html

HashMap中 get(K key) 和 put(K key,V value) 的具体过程

…衆ロ難τιáo~ 提交于 2020-03-22 18:08:21
说在前面 本文包含手写泛型HashMap<K,V>为简化版,仅为理解 HashMap 的 get() 和put() 方法的工作过程,非Java源码。 get(K key) 原理 先计算出key对应的hash值 int hash = key.hashCode(); //此处的 hashCode() 方法为 Object对象 所属方法,默认都有 //自定义的类需要覆写该方法 对超出数组范围的hash值进行处理 hash = (hash >>> 16)^hash;//java内部自做的优化,为了使hash值更加均衡,减少冲突 int index = hash & (table.length - 1);//对下标进行合理化,以免下标越界 //这样做可以使index在数组长度范围内的原因或者一个前提是,这里的数组的长度一定是2的n次方, //这样table.length - 1 在二进制情况下,除最高位,其余低位为一定是1,用hash与这样的一个数进行与操作 //即只保留了hash的二进制的低位,就会使hash的范围一定小于数组长度 根据正确的hash值(下标值)找到所在的链表的头结点 Entry<K,V> node = table[index]; 遍历链表,如果key值相等,返回对应的value值,否则返回null while(node != null){ if(node.key

删除链表里全部重复元素以及删除链表重复元素只保留一个

99封情书 提交于 2020-03-22 17:07:28
public class ListNodeDemo01 {/** * 删除链表中全部重复元素 * @param node 传入链表头 * @return 返回头节点指针 */ public static Node delDup(Node node) { //生成一个头节点 Node first = new Node(-1); Node cur = node; first.next = node; //找个头节点的代理节点 Node proxy = first; if(node == null) { return null; } while(cur != null && cur.next != null) { if(cur != null && cur.data == cur.next.data) { //后面可能还有2 //2,2,2,3 int val = cur.data; while(cur != null && cur.data == val) { //这里一直找到那个新的cur->3 cur = cur.next; } //直到找到3,把前面的去掉,改变链,proxy的后面就是3 proxy.next = cur; }else { //假如都不一样,那么第一个就是proxy了,链还是原来的链,proxy慢慢往后推 //1,2,3 proxy = cur; /

数据结构之线性表、栈、队列

删除回忆录丶 提交于 2020-03-22 15:04:03
线性表 一、定义 线性表是n个数据元素的有限序列。 二、线性表的顺序表示和实现 即用一组地址连续的储存单元依次存储线性表的数据元素。以元素在计算机内“物理位置相邻”来表示线性表中数据元素之间的逻辑关系。即是用我们熟悉的C/C++中的数组存储数据元素。只要确定了数据元素的起始位置,就可随机地访问数据元素。但是这种表示方法不便于插入和删除元素,每插入或删除一个元素,都要移动插入或删除位置之后的数据元素在连续储存单元中的位置,而且除了重新分配内存,否则在程序运行过程中不能动态地增加储存单元的数量。 三、线性表的链式表示和实现 即用一个链表来储存数据元素。这种储存结构的特点是用一组任意的存储单元存储线性表的数据元素(可连续可不连续),它包括两个域:数据域和指针域,指针域储存下一个或上一个元素的内存地址,从而使这些元素连成一个线性表。线性表有单链表、双向链表和循环链表。 双向链表的结点中有两个指针域,其一指向直接后继,另一指向直接前继,克服了单链表的单向性缺点。 循环链表是表中最后一个结点的指针域指向头结点,整个链表形成一个环。从表中任一结点出发均可找到表中其他结点。 线性表的链式表示非常方便插入或删除元素,而且效率比较高,因为元素并不是储存在物理位置相邻的存储单元中,插入或删除元素只对表中逻辑上相邻的两个(在两端时为一个)元素有影响,对其他元素无影响。而且可在程序运行过程中“无限制