链表
hebastast
这个作者很懒,什么都没留下…
展开
-
25. Reverse Nodes in k-Group
解决这个问题我的想法就是 进行计数 当计数 每次当count=1时记为head count=k时记为tail 然后反转 连入原链表 最后要记得的是 要把最后没有进行反转的地方 也加入到链表中class Solution {public: ListNode* reverseKGroup(ListNode* head, int k) { if(!head||!head->ne原创 2016-04-25 11:38:34 · 345 阅读 · 0 评论 -
21. Merge Two Sorted Lists
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode *prenode =new ListNode(0); ListNode *tail=prenode; while(l1&&l2) {原创 2016-04-23 10:01:43 · 268 阅读 · 0 评论 -
24. Swap Nodes in Pairs
class Solution { public: ListNode* swapPairs(ListNode* head) { if(!head||!head->next) return head; ListNode *headptr=new ListNode(0); headptr->next=head;原创 2016-04-23 10:02:48 · 323 阅读 · 0 评论 -
23. Merge k Sorted Lists
本来想把第一个依次和其他所有的链表进行合并的 可惜超时了 class Solution {private: ListNode* mergeTwoList(ListNode *l1,ListNode *l2) { ListNode *prenode=new ListNode(0); ListNode *tail=prenode; wh原创 2016-04-23 11:10:46 · 444 阅读 · 0 评论 -
2. Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* add原创 2016-09-10 15:27:00 · 335 阅读 · 0 评论 -
56. Merge Intervals
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */class Sol原创 2016-09-12 14:19:10 · 281 阅读 · 0 评论 -
168. Excel Sheet Column Title
class Solution {public: string convertToTitle(int n) { string result; while(n) { char ch=(n-1)%26+'A'; n=(n-1)/26; result=ch+result;原创 2016-10-09 14:36:29 · 438 阅读 · 0 评论 -
206. Reverse Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* rev原创 2016-10-13 09:51:05 · 237 阅读 · 0 评论 -
203. Remove Linked List Elements
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* rem原创 2016-10-13 10:41:04 · 237 阅读 · 0 评论 -
284. Peeking Iterator
// Below is the interface for Iterator, which is already defined for you.// **DO NOT** modify the interface for Iterator.class Iterator { struct Data; Data* data;public: Iterator(const v原创 2016-11-14 10:43:35 · 348 阅读 · 0 评论 -
328. Odd Even Linked List
class Solution {public: ListNode* oddEvenList(ListNode* head) { if(!head) return head; ListNode *odd=head; ListNode *even=head->next; ListNode *evencopy原创 2016-12-01 11:43:57 · 248 阅读 · 0 评论 -
128. Longest Consecutive Sequence
题目要求找到所有连续的数字的个数 本来想先排序 然后看前面的一个数字是不是等于当前数字-1 然后进行计数 但是排好序后复杂度就是nlogn了 所以想了另外一种方法 扫描每个数a 然后寻找a-1 a-2 …. a+1 a+2…. 把比a大的和比a小的相加 就得到了连续的个数 来更新maxlen 但是 有个问题 就是怎么确定a-1在不在数组中 这里使用hash表 这样寻找每原创 2016-05-07 15:22:12 · 378 阅读 · 0 评论 -
86. Partition List
class Solution {public: ListNode* partition(ListNode* head, int x) { if(!head||!head->next) return head; ListNode *less=NULL; ListNode *more=NULL; ListN原创 2016-04-21 11:23:44 · 409 阅读 · 0 评论 -
19. Remove Nth Node From End of List
class Solution {public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode *temp=head; int count=0; while(temp) { count++; temp=te原创 2016-04-26 09:35:13 · 256 阅读 · 0 评论 -
92. Reverse Linked List II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {private: ListNode *re原创 2016-04-26 10:06:29 · 246 阅读 · 0 评论 -
Insertion Sort List
题目很简单 就是把链表拆分成已经排好序的 ,没有排序的 ,从没有排序的中不断的取元素插入到排好序的链表中class Solution {public: ListNode* insertionSortList(ListNode* head) { if(!head||!head->next) return head; ListNode *原创 2016-04-14 22:32:22 · 436 阅读 · 0 评论 -
143. Reorder List
class Solution {public: void reorderList(ListNode* head) { int n=countList(head);//n 从0开始 如果head中只有一个元素的话返回值为0 if(n==0) return; if(n%2==0)//n为偶数 {原创 2016-04-19 10:59:23 · 352 阅读 · 0 评论 -
Linked List Cycle
问题是怎么确定链表中是否有环 使用一个快指针fast 还有一个慢指针 slow slow一次走一步 fast一次走两步 这样如果链表中有环 slow和fast则会相遇在z点 slow走了a+b fast走了a+b+c+b 问题扩展 : 1.如果有环 怎么求出环的长度 2.如果有环 怎么把环变成单链表 3.如何判断 两个链表是否相交1.因为fast走过的长度为slow的两倍原创 2016-04-20 09:53:54 · 284 阅读 · 0 评论 -
Linked List Cycle II
原理在linked list cycle 中已经叙述过了 这里不再叙述class Solution {public: ListNode *detectCycle(ListNode *head) { ListNode *fast=head; ListNode *slow=head; if(!head) return fal原创 2016-04-20 10:07:46 · 269 阅读 · 0 评论 -
138. Copy List with Random Pointer
这个一个人链表的深度拷贝 old链表为 第一步 在每一个old节点的后面加上新的copy节点 ,形成新的链表如下图所示 copy节点中的random指针指向 它要复制的old节点 next指针指向 要复制节点中的old节点中的next 第二步 更新copy节点中random指针值 copy节点中的random指针现在指向 它要copy的节点 如果copy->random->r原创 2016-04-20 11:52:38 · 599 阅读 · 0 评论 -
61. Rotate List
#include <iostream>using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };class Solution {public: ListNode* rotateRight(Li原创 2016-04-21 09:45:09 · 349 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List
class Solution {public: ListNode* deleteDuplicates(ListNode* head) { if(!head||!head->next) return head; ListNode *last;//指向去除重复的最后一个 ListNode *scan;//指向正在扫描的元素原创 2016-04-21 10:03:57 · 247 阅读 · 0 评论 -
82. Remove Duplicates from Sorted List II
要注意这个链表的第一个元素也有可能被删除掉 所以加了一个空的前驱指针#include <iostream>using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };class Solution {publ原创 2016-04-21 11:00:24 · 574 阅读 · 0 评论 -
381. Insert Delete GetRandom O(1) - Duplicates allowed
public class RandomizedCollection { private List<Integer> nums; private Map<Integer,Set<Integer>> indexs; private Random r; /** Initialize your data structure here. */ public Random原创 2017-01-05 16:14:55 · 280 阅读 · 0 评论