随笔分类 -  数据结构 / 链表

摘要:slow 一次走一步,fast 一次走两步。 那么当 fast 到达链表的末尾时,slow 必然位于中间。 ListNode* middleNode(ListNode* head) { ListNode* slow = head; ListNode* fast = head; while (fast 阅读全文
posted @ 2022-10-06 11:59 lwx_R 阅读(17) 评论(0) 推荐(0) 编辑
摘要:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(l1 == NULL){ if(l2 == NULL){ return NULL; }else{ return l2; } } if(l1 != NULL && l2 == NULL){ 阅读全文
posted @ 2022-10-06 11:47 lwx_R 阅读(7) 评论(0) 推荐(0) 编辑
摘要:ListNode* reverseList(ListNode* head) { ListNode* tail=NULL; ListNode* front=head; ListNode* curr=NULL; //先令curr指向front,front移动下一个,curr的next指向tail实现反转 阅读全文
posted @ 2022-10-06 11:47 lwx_R 阅读(12) 评论(0) 推荐(0) 编辑
摘要:ListNode* getKthFromEnd(ListNode* head, int k) { ListNode* fast=head; ListNode* slow=head; //因为头结点开始 所以要从1开始 for(int i=1;i<k;i++){ fast=fast->next; // 阅读全文
posted @ 2022-10-06 11:45 lwx_R 阅读(12) 评论(0) 推荐(0) 编辑
摘要:ListNode* deleteNode(ListNode* head, int val) { if(head->val == val){ head=head->next; return head; } ListNode* front=head->next; ListNode* tail=head; 阅读全文
posted @ 2022-10-06 11:44 lwx_R 阅读(45) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示