由买买提看人间百态

topics

全部话题 - 话题: listnode
1 2 3 4 下页 末页 (共4页)
n*****g
发帖数: 178
1
请问leetcode上这个表达是什么意思:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
不懂这一行:
ListNode(int x) : val(x), next(NULL) {}
求指教!谢谢!
d**e
发帖数: 6098
2
类似
class ListNode(int x) {
this.value = x;
this.next = NULL;
}
j*******e
发帖数: 1058
3
来自主题: JobHunting版 - M家 onsite 悲剧,同胞们弄死烙印吧
我的解法,是一个普遍的k解法。在main里面把k改为2或者是面试官喜欢的k的值就ok。
希望大家指正。
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NO... 阅读全帖
j*******e
发帖数: 1058
4
来自主题: JobHunting版 - M家 onsite 悲剧,同胞们弄死烙印吧
我的解法,是一个普遍的k解法。在main里面把k改为2或者是面试官喜欢的k的值就ok。
希望大家指正。
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NO... 阅读全帖
s*w
发帖数: 729
5
来自主题: JobHunting版 - [leetcode] merge k lists 求助
不知道为啥,我的 heap 时灵时不灵的?请看下面的 code 和输出显示
#include
#include
#include
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int v):val(v),next(NULL) {}
};
bool minHeapPredicate(ListNode* lhs,ListNode* rhs) {
return lhs->val > rhs->val;
}
class Solution {
public:
ListNode *mergeKLists(vector lists) {
ListNode *retHead = NULL, *retTail = NULL;
// store values and nodes into heap
vector ... 阅读全帖
R*****i
发帖数: 2126
6
来自主题: JobHunting版 - 大牛们帮忙,Rverse Nodes in k-Group
我用C++测试了一下,好象没问题。
class ListNode
{
public:
int value;
ListNode *next;
ListNode(int v)
{
value = v;
next = 0;
}
};
ListNode* reverseKGroup(ListNode*, int);
ListNode* reverseLinkedList(ListNode*, ListNode*);
void main()
{
ListNode *ln = new ListNode(1);
ListNode *cur = ln;
for(int i=2; i<=11; i++)
{
ListNode *lnNew = new ListNode(i);
cur->next = lnNew;
cur = lnNew;
}
ln = reverseKGroup(ln, 4);
}
ListNode* reverseKGroup(List... 阅读全帖
m*****r
发帖数: 37
7
来自主题: Programming版 - Java弱弱请救几个小问题
我是最近刚转Java的弱弱,有几个小问题,请牛牛们轻拍。
这次转java,总算像是某位说的那样,原来c++转java也就分分钟的事儿。写得顺的时
候,边google边写,也可以写得稀里糊涂、腾云架雾、行云流水;可问题是bug一出现
,立马歇菜!比如,min stack, stack1.peek()==stack2.peek(),顶上的两个数字明明
是相等的,为什么会return false呢?想去google都不知道该搜什么。。。等把java刷
一遍lc就算我可以写java了?
我有个function, public boolean dfdjdf(){return dfkld;} 为什么它一定要我在最
后一句话加个return啊,方法里的逻辑到那一步早就应该已经return了啊?
还有就是,刷lc搭了个local的架,本来没什么心理障碍,可是每遇到有Node,
ListNode, TreeNode的题就一股无名火,我不知道该把这些定义的类塞到哪里?试过两
种:
public class getIntersectionNode {
public class ListNode {
... 阅读全帖
b*******n
发帖数: 8
8
来自主题: JobHunting版 - leetcode Sort List
做了这道题,被accepted, 但觉得代码很罗嗦冗长。
怎么改进呢?
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
class ListNodeWrapper {
ListNode start;
ListNode end;
}
public class Solution {
private ListNode partionList(ListNode start, ListNode end) {
ListNode end1 = start;
ListNode fast = start;
while (fast != end) {
fast = fast.next;
... 阅读全帖
b******p
发帖数: 49
9
来自主题: JobHunting版 - leetcode上的Sort List那道题
我来贴个CPP的(注意:以下有乱七八糟的code……)
合并排序确实比较好用,我还在写第一遍leetcode,代码风格也比较乱,带了很多
debug code,还带了测试用例,试了9次才通过…
有一些多边界条件需要判断的。我的方法是加很多debug,或加很多assertion(我做别
的题里面经常用assertion……不知道是不是好习惯)
============================
#include
#include
using namespace std;
/*
Merge sort ?
*/
// Start: 22:40
// End: 23:45 用了一个小时
//Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
#define TOMMYDBG
class Solution {
public:
... 阅读全帖
h****n
发帖数: 1093
10
来自主题: JobHunting版 - 合并两个排序好的链表, 优解?
写了三个方法,一个是递归,一个是naive方法,一个是用指向指针的指针来构建
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode* res;

//Method using pointer to pointer
res = P2PMethod(l1,l2);
//res = recursive(l... 阅读全帖
a**********0
发帖数: 422
11
来自主题: JobHunting版 - 关于reorder list 的总结
我本来用递归方法 但是超时了 本身代码没错 就是慢 为什么慢 因为在每个递归函数
中有O(n)的操作 这样n次递归导致总体O(n平方)
如果坚持递归方法 需要返回调整好的list的尾节点的下一个节点 这个是从网友那里学
来的 其实这个过程有点tricky 为什么不简单返回 list的头节点或者尾节点 而要返
回尾节点的下一个节点呢? 思路是这样的 尾节点经过调整已经不是原来的尾节点了
它指向的下一个已经不是原来的顺序 那为什么不返回头节点呢 ? 头节点其实不需要
返回 每次用next找就可以了 而且每次拿到头节点也不好用 必须多次next找到尾节点
又超时了
网友的方法 我改编成了java的 后边也有返回尾节点的方法
但是很容易出错 如果面试 还是用循环而不是递归的方法 当然面试无法检查是不是超
时 所以用我最早的方法也可以吧
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* ... 阅读全帖
h****n
发帖数: 1093
12
来自主题: JobHunting版 - M家 onsite 悲剧,同胞们弄死烙印吧
我的递归和非递归解法,贴在下面 OJ通过 ,还有每K个reverse那个,不过那个只有递
归版本
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
/*
//递归做法很简单,但是要切记前面几行的顺序,别放错了
ListNode* cur = head;
if(cur==NULL) return NULL;
ListNode* n... 阅读全帖
h****n
发帖数: 1093
13
来自主题: JobHunting版 - M家 onsite 悲剧,同胞们弄死烙印吧
我的递归和非递归解法,贴在下面 OJ通过 ,还有每K个reverse那个,不过那个只有递
归版本
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *swapPairs(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
/*
//递归做法很简单,但是要切记前面几行的顺序,别放错了
ListNode* cur = head;
if(cur==NULL) return NULL;
ListNode* n... 阅读全帖
g*********e
发帖数: 14401
14
来自主题: JobHunting版 - leetcode Sort List
我的也很长,但不用每个recursion都寻找中间node的位置。
class Solution {
public:
ListNode *merge(ListNode *a, ListNode *b, ListNode *&last) {
ListNode *res=NULL;
ListNode *prev=NULL;
while(a && b) {
if(res==NULL)
res=(a->val < b->val) ? a:b;
if(a->val < b->val) {
if(prev)
prev->next=a;
prev=a;
a=a->next;
} else {
if(prev)
prev->next=b;... 阅读全帖
f**********3
发帖数: 295
15
来自主题: JobHunting版 - leetcode上的Sort List那道题
public class Solution {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode tail = head.next;
while (tail.next != null) {tail = tail.next;}
HeadTail ret = sort(head, tail);
return ret.head;
}

class HeadTail {
ListNode head;
ListNode tail;
public HeadTail(ListNode head, ListNode tail) {
this.head = head;
this.tail = tail;
}
}

... 阅读全帖
h*********o
发帖数: 230
16
来自主题: JobHunting版 - 大牛们帮忙,Rverse Nodes in k-Group
Rverse Nodes in k-Group:
看了好久,没发现问题在哪儿,求大牛们过目~~
谢了!
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
// Start typing your Java solution below
// DO NOT write main() function
if(head==null)
return null;
ListNode begin=head;
ListNode pre=null;

ListNode end=null;
while(begin!=null){
ListNode cur=begin;
for(int i=0;i cur=cur.next;
... 阅读全帖
d****n
发帖数: 233
17
来自主题: JobHunting版 - leetcode Sort List
Similar to yours but shorter:
ListNode *merge(ListNode *a,ListNode *b) {
ListNode sentil(0);
ListNode *r = &sentil;

while (a && b) {
if (a->val > b->val) {
r->next = b;
b = b->next;
} else {
r->next = a;
a = a->next;
}
r = r->next;
}
if (a) r->next = a;
if (b) r->next = b;
return sentil.next;
}

ListNode *mySort... 阅读全帖
c*******7
发帖数: 438
18
来自主题: JobHunting版 - 请教iterative merge sort list的代码
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode sortList(ListNode head) {

if(head == null) {
return null;
}
ListNode[] heads = new ListNode[100];
int[] counts = new int[100];
heads[0] = head;
counts[0] = 1;

ListNode next = head.next;
i... 阅读全帖
c*****a
发帖数: 808
19
来自主题: JobHunting版 - 请教各位大牛一个K-way merge 的问题
我的用堆
public ListNode mergeKLists(ArrayList lists) {
Comparator mycomp = new Comparator(){
@Override
public int compare(ListNode a, ListNode b){
if(a.val else if(a.val==b.val) return 0;
else return 1;
}
};
if(lists.size()==0) return null;
PriorityQueue heap = new PriorityQueue(lists.
size(), mycomp);
for... 阅读全帖
b******7
发帖数: 92
20
来自主题: JobHunting版 - 求两个程序的C++ code
1. merger sort a single linked list
template
ListNode * mergeHelper(ListNode * left, ListNode * right)
{
assert(left != NULL && right != NULL);
ListNode * head = NULL;
if(left->val < right->val)
{
head = left;
left = left->next;
}
else
{
head = right;
right = right->next;
}
while( left != NULL && right != NULL)
{
if(left->val < right->val)
{
... 阅读全帖
h*z
发帖数: 33
21
来自主题: JobHunting版 - leetcode Sort List
C++ looks shorter.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *sortList(ListNode *head) {
if (!head || !head->next) return head;

ListNode *slow = head;
ListNode *fast = head;
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}

Lis... 阅读全帖
c********w
发帖数: 2438
22
来自主题: JobHunting版 - leetcode Sort List
我po个我的
public class Solution {
public ListNode sortList(ListNode head) {
if(head==null||head.next==null)
return head;

ListNode slow=head;
ListNode fast=head.next;
while(fast!=null){
fast=fast.next;
if(fast!=null){
fast=fast.next;
slow=slow.next;
}
}

ListNode h1=head;
ListNode h2=slow.next;
slow.next=null;

h1=sortLi... 阅读全帖
m*****r
发帖数: 37
23
来自主题: Programming版 - Java弱弱请救几个小问题
弄成这样的了:
// ./com/ListNode.java
package com;
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) {
val = x;
next = null;
}
}
// ./hasCycle.java
import com.ListNode;
public class hasCycle {
public static class Solution {
public boolean hasCycle(ListNode head) {
if (head == null || (head != null && head.next == null))
return false;
else if (head != null && head.next == head)
... 阅读全帖
c****7
发帖数: 13
24
来自主题: JobHunting版 - 请教各位大牛一个K-way merge 的问题
原题是leetcode上的merge k sorted lists
http://discuss.leetcode.com/questions/204/merge-k-sorted-lists
这个题有好几种解法,比如divide and conquer, priority queue.
我写了个priority queue的。我的问题是, 如果现在要做 merge k sorted
array,怎样来设计这个priority queue才能最简单有效呢?
附上 merge-k-sorted-lists的代码
public static ListNode mergeKLists_priorityQueue(ArrayList kLists)
{
// border case
if(kLists.size()==0) return null;
if(kLists.size()==1) return kLists.get(0);
// create a Comparator based on the nod... 阅读全帖
a***e
发帖数: 413
25
这些题看着容易,老是写不对或者代码很冗长,有大牛们指点一下如何提高么?
呜呜,不知道哪年哪月才能刷完一遍,有同学互勉一下么?
比如
Given a linked list and a value x, partition it such that all nodes less
than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the
two partitions.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
我的
Runtime Error

Last executed input:
{1}, 0
ListNode *partition(ListNode *head, int x) {
... 阅读全帖
h*****g
发帖数: 944
26
执行的时候总说segmentation fault,
谢谢大家指点
===================================
#include
using namespace std;
template< class T>
class ListNode{
public:
T data;
ListNode(T v);
ListNode< T > *next;
};
template ListNode < T >::ListNode(T v)
{
}
template
class LinkedList{
ListNode *head;
public:
LinkedList(){}
~LinkedList(){
while(head){
ListNode* next = head->next;
delete head;
... 阅读全帖
c****7
发帖数: 13
27
来自主题: JobHunting版 - 请教各位大牛一个K-way merge 的问题
各位大牛,请看看下面这个算法, 这个是不用heap的算法。
我觉得complexity 是 O(k*n), k是list 的个数, n 是list的平均长度。
那是不是这个应该比用heap的更好点啊?
public static ListNode mergeKLists(ArrayList kLists) {
if (kLists==null)
return null;
if (kLists.size()==0)
return null;
// if there is only one list in kLists
if (kLists.size()==1)
return kLists.get(0);
int length = kLists.size();
// initialize the curList with the first item in the list
ListNo... 阅读全帖
W********e
发帖数: 45
28
我的办法就是进行二分,将k个链表分为两个一组,组内进行merge。形成一个新的链表
集合。继续两个一组merge,这样下去一共会进行logk次merge,最后merge成为一个链
表。这里用的辅助函数是mergeSortedList,合并两个有序链表,这个辅助函数复杂度
应该是O(n)。
我觉得这个算法的总时间复杂度是O(nlogK),大家觉得对吗??
class Solution {
public:
ListNode* mergeSortedList(ListNode*l1,ListNode*l2)
{
ListNode *h1=l1,*h2=l2;
ListNode *newHead=new ListNode(0),*dummy=newHead; //newHead要赋
值,否则没有next。如果是C语言的话可以申请stack的对象
if(l1==NULL&&l2==NULL)
return NULL;
while(h1!=NULL&&h2!=NU... 阅读全帖
b******7
发帖数: 92
29
来自主题: JobHunting版 - 透露两个G的onsite题
ListNode * add(ListNode * a, ListNode * b)
{
if(a == NULL || b == NULL) return NULL;
ListNode * head = NULL;
ListNode * pre = NULL;
ListNode * preLess9 = NULL;
for(; a != NULL; a = a->next, b = b->next)
{
ListNode * temp = new ListNode(a->val, b->val);
if(pre != NULL)
pre->next = temp;
else
head = temp;
... 阅读全帖
w****r
发帖数: 15252
30
来自主题: JobHunting版 - [ 每日一课] Sort List
/*
* Sort a linked list in O(n log n) time using constant space
complexity.
*/
public ListNode sortList(ListNode head) {
if(head == null || head.next == null)
return head;

//get the length of the liklist
int count = 0;
ListNode node = head;
while(node!=null){
count++;
node = node.next;
}


//break up to two list
int middle = count / 2;
... 阅读全帖
b*********h
发帖数: 103
31
来自主题: JobHunting版 - 请教各位大牛一个K-way merge 的问题
看大家都用优先队列,贴一个 set 的吧 呵呵,用 C++ 的表示声明 priority queue
打字太多。。。继续说 C++ 的 priority queue 又不支持 decrease key,喜欢用 set
当 priority queue 。。。
class Solution {
public:
ListNode *mergeKLists(vector &lists) {
multiset S;
for (int i = 0; i < lists.size(); ++i) {
if (lists[i]) {
S.insert(lists[i]);
}
}
ListNode* head = 0;
ListNode* tail = 0;
while (!S.empty()) {
ListNode* nod... 阅读全帖
p*****p
发帖数: 379
32
来自主题: JobHunting版 - leetcode 上的k way merge
heap直接放node就行了啊,贴个java的,c++也一样的
public class Solution {
public ListNode mergeKLists(ArrayList lists) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode root = null;
ListNode current = null;
if (lists.isEmpty()) return null;
Queue queue = new PriorityQueue(lists.size(), new
Comparator() {
public int compare(ListNode n1, ListNode n2) {
return n1.val - n2.val;... 阅读全帖
j**7
发帖数: 143
33
来自主题: JobHunting版 - 明天电面,求建议
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode head1, ListNode head2) {

if(head1==null &&head2==null)
{
return null;
}
if(head1==null)
return head2;
if(head2==null)
return head1;

ListNode head=null;
ListNode result... 阅读全帖
m**p
发帖数: 189
34
为什么泥?
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode* dummy = new ListNode(-1);
ListNode* pivot = new ListNode(x);
ListNode* dummy_tail = dummy;
ListNode* pivot_tail = pivot;
ListNode* cur = head;
while (cur) {
if (cur->val < x) {
dummy_tail->next = cur;
dummy_tail = dummy_tail->next;
} else {
... 阅读全帖
a**********0
发帖数: 422
35
来自主题: JobHunting版 - reorder list 递归方法超时
不递归无非就是分成两半 后一办reverse 然后插入到第一份去 我懒得写这么多代码
于是用了递归方法 结果超时 在自己电脑上跑了几个小case 都过了 不知道大家什么看
法 又超时了!
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {

public void reorderList(ListNode head) {

if(head == null)
return;

if(head.next ==null)
return;

if(head.next.next == null)
ret... 阅读全帖
c**z
发帖数: 669
36
来自主题: JobHunting版 - LRU cache 超时, 大家帮忙看看
public class LRUCache {
private HashMap m = new HashMap(
);
private LinkedList l = new LinkedList();
private int capacity;

public LRUCache(int capacity) {


this.capacity = capacity;
}

public int get(int key) {
if ( m.containsKey(key) ){
moveListNodetoFront(key);
return m.get(key).value;
}
return -1;
}

public void set(int ke... 阅读全帖
m*********g
发帖数: 170
37
来自主题: JobHunting版 - 请教一个面试算法题
ListNode
{
int id; //assuming be non-negative
float weight;
ListNode* next;
}
merge(ListNode* l1, ListNode* l2)
{
ListNode l1head, h2head, l3head;
l1head->next = l1;
l2head->next = l2;
ListNode* l3 = &h3head;

//use hashtable to keep track of the id->weight pair.
unordered_map m;

// populate with l1 list
while(l1 != NULL)
{
m.insert(pair(l1->id, l1->weight));
l1 = l1->next;
}
//c... 阅读全帖
j********r
发帖数: 25
38
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode dummy(0);
ListNode dummy2(0);
ListNode *p = &dummy;
ListNode *pp = &dummy2;
while(head) {
if (head->val < x) {
p->next = head;
p = p->next;
}
else {
... 阅读全帖
d********e
发帖数: 239
39
来自主题: JobHunting版 - 请教iterative merge sort list的代码
ListNode *sortList(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL || head->next == NULL) {
return head;
}

ListNode* mid = head;
ListNode* end = head;
while(end->next){
end = end->next;
if (end->next == NULL)
break;
end = end->next;
mid = mid->next;... 阅读全帖
a***e
发帖数: 413
40
Update:
终于写出来了。。。。。去掉prev2 = p->next;就对了。
还是欢迎大牛们指点,讨论哈!哎,不知哪年哪月才能练到能去面试的。。。。。。
独自刷题非常郁闷。
Reverse Nodes in k-Group
https://oj.leetcode.com/problems/reverse-nodes-in-k-group/
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 end
should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Give... 阅读全帖
C*******n
发帖数: 193
41
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *removeNthFromEnd(ListNode *head, int n) {
if (n==0 || !head){
return NULL;
}

ListNode* res = new ListNode(0);
res->next = head;
head = res;

ListNode* p=head;
ListNode* q=head;

for (int i=0;i<=n;i++){
... 阅读全帖
f****e
发帖数: 923
42
来自主题: JobHunting版 - java 链表里面dummy node 一问?谢谢
刚刚学习链表, 请问 head = dummy 是把dummy的值赋给 head吗? 还是其他含义?
为什么最后返回dummy.next 就是返回合并之后的整个链表?貌似 循环过程没dummy 啥
事?
private ListNode merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(-1);
ListNode head = dummy;
while (head1 != null && head2 != null ) {
if (head1.val < head2.val) {
head.next = head1;
head1 = head1.next;
}else {
head.next = head2;
head2 = head2.next;
... 阅读全帖
f****e
发帖数: 923
43
class ListNode{
int val;
ListNode next;
ListNode(int val){
this.val = val;
}
}
class Solution{
public int live(int n, int k){
ListNode head = new ListNode(1);
ListNode node = head;
for(int i = 2; i <= n; i++){
node.next = new ListNode(i);
node = node.next;
}
node.next = head;
System.out.println("before remove");
printList(head);
System.out.println("removing the nodes");
whi... 阅读全帖
f****e
发帖数: 923
44
来自主题: Programming版 - java 链表里面dummy node 一问?谢谢
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
刚刚学习java,请问 head = dummy 是把dummy的值赋给 head吗?
为什么最后返回dummy.next 就是返回合并之后的整个链表?貌似 循环过程没dummy 啥
事?
private ListNode merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(-1);
ListNode head = dummy;
while (head1 != null && head2 != null ) {
if (head1.val < head2.val) {
head.next =... 阅读全帖
g***j
发帖数: 1275
45
来自主题: JobHunting版 - 关于priority_queue一问
做leedcode上面的关于k-way merge的题目,题目是
Merge k sorted linked lists and return it as one sorted list.
如下的code通过不了,似乎是因为我在priority_queue里面用了指针
std::priority_queue, CompareNode> myQ;
因为如果我改成
std::priority_queue, CompareNode> myQ;
并且把其他的相关->都改了dot之后,就可以全部通过了。
我用debug跟踪,发现myQ.pop()之后,虽然myQ.size()表小了,但是myQ.top()的内容
没有发生变化。
请问,这里面的trick是什么?难道这里不能用Node*么?如果是这样的,还有什么
container不能用Node*的?
大牛帮我解释一下好么?
class Pair{
public:
ListNode* node;
int index;
};
class CompareNo... 阅读全帖
g***j
发帖数: 1275
46
来自主题: JobHunting版 - 关于priority_queue一问
这个code就可以全部test case都通过。改动就是指针和非指针的差别。
class Pair{
public:
ListNode* node;
int index;
};
class CompareNode: public std::binary_function{
public:
bool operator()(const Pair p, const Pair q) const {
return p.node->val > q.node->val;
}
};
class Solution {
public:
ListNode *mergeKLists(vector &lists) {

if(lists.size() == 0) return NULL;
if(lists.size() == 1) return lists[0];

std::priority_queu... 阅读全帖
p*****2
发帖数: 21240
47
来自主题: JobHunting版 - leetcode 关于Partition List
我刚写了一个,没发现有什么问题。就是leetcode现在很慢。
public ListNode partition(ListNode head, int x) {
ListNode dummy=new ListNode(0);
dummy.next=head;
ListNode newDummy=new ListNode(0);
ListNode p1=dummy;
ListNode p2=newDummy;

while(p1.next!=null){
if(p1.next.val p2.next=p1.next;
p2=p2.next;
p1.next=p1.next.next;
}
else
p1=p1.next;

... 阅读全帖
a**********2
发帖数: 340
48
修修补补好几次才过了测试,写的又臭又长,谁能帮我改一下啊?
class Solution {
public:
ListNode *reverseBetween(ListNode *head, int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if( m > n || !head) return NULL;

ListNode* tail1 = (m>1?head:NULL);
ListNode* head2 = NULL;
ListNode* tail2 = NULL;
int i;
for( i = 1; i < m-1; i++)
{
if(!tail1)return NULL;
tail1=tail1->next;
}
ListN... 阅读全帖
r*******n
发帖数: 3020
49
来自主题: JobHunting版 - 面试题
这个题有点意思。
ListNode* insert(ListNode* head){
if(head==NULL)
return;
ListNode* curr=head->next;
ListNode* dummyHead= new ListNode(0);
dummyHead->next=head;
while(curr!=NULL){
ListNode* pre=dummyHead;
// Find where to insert
while(p!=curr){
if(p->val>=curr->val)
break;
pre=p;
p=p->next;
}
if(p==curr){
//curr's value is largest so far
curr=curr->next;
}else{
//d... 阅读全帖
b*********s
发帖数: 115
50
非大牛
我是在最开始加一个dummy head
public class Solution {
public ListNode insertionSortList(ListNode head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ListNode dummy = new ListNode(Integer.MIN_VALUE);
dummy.next = head;
ListNode preNode = dummy;
ListNode curNode = dummy.next;
while (curNode != null) {
ListNode pre = dummy;
ListNode cur = dum... 阅读全帖
1 2 3 4 下页 末页 (共4页)