摘要:
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?Solution:首先选取一快一慢两个指针fast与slow: fast每步走两个结点,slow每步走一个结点。若链表有环,则fast将会在某一个时刻追上slow。1. 假设链表有环,且链表长度为n,循环从第k + 1个结点开始(亦即走k步进入循环)2. 让两个指针fast, slow从链表头同时开始遍历 阅读全文
摘要:
Sort a linked list using insertion sort.Solution:新建链表,逐个插入即可~/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *insertionSortList(ListNode *head) { if(head ... 阅读全文