1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * ...
分类:
编程语言 时间:
2014-07-16 17:45:41
阅读次数:
226
Sort a linked list using insertion sort.
class Solution {
public:
ListNode *insertionSortList(ListNode *head) {
if(head == NULL || head->next == NULL)
return head;
Lis...
分类:
其他好文 时间:
2014-07-15 12:22:53
阅读次数:
244
题目来源,待字闺中,原创@陈利人
,欢迎大家继续关注微信公众账号“待字闺中”
分析:思路和数据的快速排序一样,都需要找到一个pivot元素、或者节点。然后将数组或者单向链表划分为两个部分,然后递归分别快排。
针对数组进行快排的时候,交换交换不同位置的数值,在分而治之完成之后,数据就是排序好的。那么单向链表是什么样的情况呢?除了交换节点值之外,是否有其他更好的方法呢?可以修改指针,不进行数值...
分类:
其他好文 时间:
2014-07-14 20:48:18
阅读次数:
280
Sort a linked list inO(nlogn) time using constant space complexity.题解:实现一个链表的归并排序即可。主要分为三部分:1.找到中点并返回的函数findMiddle;2.归并函数merge;3.排序函数sortList。数组的findM...
分类:
其他好文 时间:
2014-07-14 20:00:56
阅读次数:
182
Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For ...
分类:
其他好文 时间:
2014-07-14 19:33:04
阅读次数:
215
Sort a linked list using insertion sort.题解:实现链表的插入排序。要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻烦。所以每次利用kepeler.next.val和head.val比较大小,而不是kepeler.val...
分类:
其他好文 时间:
2014-07-14 17:47:19
阅读次数:
207
今天做了Leetcode上面一道题Remove Duplicates from Sorted List II,要去去除链表中重复的节点,代码如下 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * ...
分类:
其他好文 时间:
2014-07-13 21:23:10
阅读次数:
160
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ...
分类:
其他好文 时间:
2014-07-13 16:32:04
阅读次数:
152
题目来源,待字闺中,原创@陈利人
,欢迎大家继续关注微信公众账号“待字闺中”
原题:两个单链表(singly linked list),每一个节点里面一个0-9的数字,输入就相当于两个大数了。然后返回这两个数的和(一个新list)。这两个输入的list长度相等。 要求是:不用递归;要求算法在最好的情况下,只遍历两个list一次
,最差的情况下两遍
分析:如果链表表示的数是从...
分类:
其他好文 时间:
2014-07-12 23:47:33
阅读次数:
285
Problem Description:
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the...
分类:
其他好文 时间:
2014-07-12 19:00:26
阅读次数:
235