![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
链表
Faith★
这个作者很懒,什么都没留下…
展开
-
二进制链表转整数
class Solution {public: int getDecimalValue(ListNode* head) { int ans = 0; while(head) { ans = (ans << 1) + head -> val; head = head->next; } return ans; }};原创 2020-11-26 14:42:41 · 68 阅读 · 0 评论 -
链表的中间结点
class Solution {public: ListNode* middleNode(ListNode* head) { ListNode *p = head; int n = 0; int k = 0; while(p) { p = p->next; n++; } k = n / 2; for(int i = .原创 2020-11-26 14:42:33 · 73 阅读 · 0 评论 -
删除链表中的节点
class Solution {public: void deleteNode(ListNode* node) { node -> val = node -> next -> val; node -> next = node -> next -> next; }};原创 2020-11-26 14:42:26 · 43 阅读 · 0 评论 -
回文链表
class Solution {public: bool isPalindrome(ListNode* head) { ListNode *t = head; stack<int> s; while(t){ s.push(t->val); t = t->next; } t = head; while(t){ .原创 2020-11-26 14:42:20 · 36 阅读 · 0 评论 -
反转链表
class Solution {public: ListNode* reverseList(ListNode* head) { ListNode *cur = NULL; ListNode *pre = head; while(pre){ ListNode *t = pre->next; pre->next = cur; cur = pre; .原创 2020-11-26 14:42:14 · 38 阅读 · 0 评论 -
移除链表元素
class Solution {public: ListNode* removeElements(ListNode* head, int val) { if(head == NULL) return NULL; ListNode *p = head; while(p->next) { if(p->next->val == val) { .原创 2020-11-26 14:42:07 · 37 阅读 · 0 评论 -
相交链表
class Solution {public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *l1 = headA; ListNode *l2 = headB; while(l1 != l2){ if(l1 == NULL) l1 = headB; else .原创 2020-11-26 14:41:55 · 44 阅读 · 0 评论 -
排序链表
class Solution {public: ListNode* sortList(ListNode* head) { if(head == NULL || head->next == NULL){ return head; } ListNode *fast = head; ListNode *slow = head; ListNode *mid; while(fast.原创 2020-11-26 14:41:46 · 56 阅读 · 0 评论 -
环形链表II
class Solution {public: ListNode *detectCycle(ListNode *head) { ListNode *fast = head; ListNode *slow = head; while(fast&&fast->next){ fast = fast->next->next; slow = slow->next; .原创 2020-11-26 14:41:39 · 40 阅读 · 0 评论 -
环形链表
class Solution {public: bool hasCycle(ListNode *head) { ListNode *fast = head; ListNode *slow = head; while(fast&&fast->next){ fast = fast->next->next; slow = slow->next; .原创 2020-11-26 14:41:33 · 50 阅读 · 0 评论 -
有序链表转换二叉搜索树
class Solution {public: TreeNode* sortedListToBST(ListNode* head) { return BuildTree(head, nullptr); } TreeNode *BuildTree(ListNode* head, ListNode * tail){ if(head == tail) return nullptr; ListNode *slow = head; .原创 2020-11-26 14:41:21 · 59 阅读 · 0 评论 -
合并K个升序链表
class Solution {public: ListNode* mergeKLists(vector<ListNode*>& lists) { if(lists.size()==0) return nullptr; ListNode* head = lists[0]; for(int i = 1;i < lists.size();i++){ if(lists[i]) .原创 2020-11-26 14:41:27 · 73 阅读 · 0 评论 -
合并两个有序链表
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(l1==nullptr) return l2; if(l2 == nullptr) return l1; else if(l1->val <= l2->val){ l1->next = mergeTwoList.原创 2020-11-26 14:41:15 · 43 阅读 · 0 评论 -
19. 删除链表的倒数第N个节点
删除链表的倒数第N个节点给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。示例:给定一个链表: 1->2->3->4->5, 和 n = 2.当删除了倒数第二个节点后,链表变为 1->2->3->5.class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* dummy = new ListNode.原创 2020-11-26 14:41:05 · 61 阅读 · 0 评论