- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是 LeetCode 中的一个简单问题:反向链表我有两个相似的代码,我无法弄清楚两者之间的区别,但它输出不同的结果。
第一个 while 循环的结果是正确的答案,但第二个 while 循环的答案是错误的。而不是 temp = current
。接下来,我只是将电流存储到温度在 while 循环的最后一行,我正确地将 current 切换为 temp.next。我认为他们应该得到相同的答案但是当输入 {1,2,3,4,5}
时,第二个解决方案得到了错误的答案 {1}
。
ListNode reverse = null;
ListNode current = head;
while(current != null){
ListNode temp = current.next;
current.next = reverse;
reverse = current;
current = temp;
}
这是第二个 while 循环。
while(current != null){
ListNode temp = current;
current.next = reverse;
reverse = current;
current = temp.next;
}
最佳答案
while(current != null){
// This line temp is keeping the reference of current variable
ListNode temp = current;
// now temp and current both pointing to same value
// This line will make the next of current variable pointing to reverse reference
// as current and temp were same so next of temp is also pointing to the reverse
current.next = reverse;
// now the reverse will be changed and point to the current reference
reverse = current;
// so up to here reverse, temp, current all pointing to the same location
// as all are pointing same so temp.next,curr.next and reverse.next
// will store the previous reference
// current will point back to the reverse value
// no increment is done in the whole loop
current = temp.next;
}
关于algorithm - Reverse LinkedList - 链表工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56452625/
如何在不使用Array.Reverse()方法的情况下反转数组(在C#中)?。例如,。应该会导致。这是我的面试任务。
我有一个内容,我希望它们以倒序排列和换行倒序排列,但顺序相反。这是代码: .a { height: 200px; width: 520px; padding: 5px 5px 5px 10
很多时候,我看到在列表头部运行的函数,例如: trimHead ('\n':xs) = xs trimHead xs = xs 然后我看到了定义: trimTail = reverse
如果 Reverse :: [k] -> [k]是一个类型族,那么 Haskell 无法判断 (Reverse (Reverse xs)) ~ xs .有没有办法让类型系统知道这一点而无需任何运行时成
我有一个单维项目数组,其声明和初始化为: string[] SubDirectorylist = Directory.GetDirectories(TargetDirectory); 我想反转成员并发
这是代码 >>> a=[1,3,2] >>> a [1, 3, 2] >>> a= 3,1,2 >>> a (3, 1, 2) >>> sorted(a) [1, 2, 3] >>> sorted(a
我对以下问题感兴趣:Collections.reverse() 与 Lists.reverse() 哪个更快? 最佳答案 他们做不同的事情。 Collections.reverse 采用可变列表并反转
我对以下问题感兴趣:Collections.reverse() 与 Lists.reverse() 哪个更快? 最佳答案 他们做不同的事情。 Collections.reverse 采用可变列表并反转
我今天在我的 Django 控制台中尝试了这个,我得到了两个不同的结果。我认为 list.reverse() 会反转列表(即第一个对象变成最后一个,依此类推)[1]。然而,情况似乎并非如此。 >>>
这个小小的 jQuery 插件: jQuery.fn.reverse = [].reverse; 它是如何工作的?对象绑定(bind)在哪里 - 反转函数的数组原型(prototype)?我真的不明白
大概都是 mylist.reverse()和 list.reverse(mylist)最终执行 reverse_slice 在 listobject.c通过 list_reverse_impl 或 P
您好,我在添加 django-reversion 和 django-reversion-compare 模块时遇到了一些问题。 我创建了新项目,我想使用 django-reversion 跟踪 use
我有以下观点: def default_new (request): if request.method == "POST": post = EquipmentForm(req
我对 List.Reverse() 有疑问和 Reverse(this IEnumerable source) .查看代码: // Part 1 List list = new List {
我需要首先对字典进行排序,值 reverse=True,对于重复值,按键排序 reverse=False 到目前为止,我有这个 dict = [('B', 3), ('A', 2), ('A', 1)
我有一个字典,其中包含要排序的字符串键和 int 值。我希望它首先按递减值编号排序,然后按字母顺序排序。 例如,如果您有一个包含以下内容的字典: my_dict = {'zebra':1, 'the'
我是编程新手。我正在通过第 9 channel 的 Bob Tabors 视频学习 c#。 你能解释一下为什么我们不能做这样的事情吗: string mijnVoornaam = "Remolino"
这个问题在这里已经有了答案: Why there is two completely different version of Reverse for List and IEnumerable? (
有什么区别 mylist = reversed(sorted(mylist)) 对 mylist = sorted(mylist, reverse=True) 为什么要使用一个而不是另一个? 如何在多
我的问题涉及将新方法添加到现有String 构造函数 的应用程序。在 Stoyan Stefanov 的 Object Oriented Program for Javascript 中,有一个使用
我是一名优秀的程序员,十分优秀!