CC150 LinkedList

来源:互联网 发布:域名投资人 编辑:程序博客网 时间:2024/05/17 08:25

CC150中的LinkedList讲的很好,之后上代码,最近太忙了


2.1 Write code to remove duplicates from an unsorted linked list. 

这道题很简单就是利用HashTable存数然后遇到之前遇到过的节点直接删除

如果No buffer used,就是只能使用O(1) space complexity, you can not extral space. 就是二维的遍历,从一个节点开始遍历之后所有的节点,如果有后面的节点和当前的节点相同就删除后面的节点。


2.2 Implement analgorithmto find the kth to last element of a singly linked list. 

Find a listNode to get the Kth last element from the linkedList,就是往后走k步,然后再开始走

也可以使用递归。如果当前节点的数字为k + 1, 就删除之后的节点


2.3 Implement an algorithm to delete a node in themiddleof a singly linked list, givenonly access tothatnode. 

这道题是你只有Access 到你想删除的元素,所以就找到那个你想删除的元素,然后把next的value赋值,然后删除next就好


2.4Write code to partition a linked list around a value x, such that all nodes less than xcome before alt nodes greater than or equal to x. 


2.5You have two numbers represented by a linked list, where each node contains asingledigit.Thedigitsarestoredinreverseorder,suchthatthe1'sdigitisat theheadof the list. Write afunction that adds the two numbers and returns the sum as alinked list. 

这道题可以写成递归的形式,就会转换成找到最后一个元素然后计算


2.6Given a circular linked list,implementanalgorithmwhich returns the node at thebeginning of the loop. 利用快慢指针,有一定的技巧性


2.7Implement a function to check if a linked list is a palindrome, 

这道题使用了Stack,slow指针的位置对于奇数长度和偶数长度的LinkedList两种情况是不一样的。




0 0
原创粉丝点击