通过链表的一些题目,了解链表的基本操作实现,掌握递归算法的基本思路,掌握扎实的编程习惯。一、单链表基本操作1.1、单链表节点定义struct ListNode{ int value; ListNode *pNext;};1.2、在尾部插入节点void AddToTail(ListNode...
分类:
其他好文 时间:
2014-07-16 20:33:24
阅读次数:
150
今天做了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
不知道为什么{3,4,1}就是通不过。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), n...
分类:
其他好文 时间:
2014-07-13 12:25:11
阅读次数:
236
题目
Sort a linked list using insertion sort.
解答
链表无法像数组那样从后往前依次比较插入,只能从前往后;在链表首部添加一个哨兵可以稍微简化下代码,代码如下:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ...
分类:
其他好文 时间:
2014-07-11 08:06:49
阅读次数:
362
def Merge(head1, head2):
if head1 == None: return head2
if head2 == None: return head1
psuhead = ListNode(-1)
tail = psuhead
while head1 and head2:
if head1.val < head2.val:
cur = head1
...
分类:
其他好文 时间:
2014-07-08 18:46:04
阅读次数:
227
def reverse(head):
if head == None or head.next == None:
return head
psuhead = ListNode(-1)
while head:
nexthead = head.next
head.next = psuhead.next
psuhead.next = head
head = nexthead
...
分类:
其他好文 时间:
2014-07-08 15:27:58
阅读次数:
183
简单的插入排序,总是超时,暂且放在这记录一下。
class Solution:
# @param head, a ListNode
# @return a ListNode
def insertionSortList(self, head):
if head == None or head.next == None:
return head
psuhead...
分类:
编程语言 时间:
2014-07-06 11:52:20
阅读次数:
230
单链表的反转可以使用循环,也可以使用递归的方式
1.循环反转单链表
循环的方法中,使用pre指向前一个结点,cur指向当前结点,每次把cur->next指向pre即可。
代码:
class ListNode:
def __init__(self,x):
self.val=x;
self.next=None;
def nonrecurse(he...
分类:
编程语言 时间:
2014-07-06 00:24:52
阅读次数:
331
用c++类将单向链表类化,保存后可以方便的进行数的排序,插入操作; 调试成功的!#include using namespace std;class ListNode//创造节点成分{public: ListNode(int datavalue)//构造函数一 { value=datavalue.....
分类:
其他好文 时间:
2014-07-05 21:48:08
阅读次数:
175