我正在尝试撤消链接列表。这是我提出的代码:
public static void Reverse(ref Node root)
{
Node tmp = root;
Node nroot = null;
Node prev = null;
while (tmp != null)
{
//Make a new node and copy tmp
nroot = new Node();
nroot.data = tmp.data;
nroot.next = prev;
prev = nroot;
tmp = tmp.next;
}
root = nroot;
}
运作良好。想知道是否有可能避免创建新节点。想对此提出建议。
答案 0 :(得分:52)
这个问题经常被问到。当我多年前在我的采访中被问到这个问题时,我的理由如下:单链表基本上是一个堆栈。因此,反转链表是对栈的一个简单操作:
newList = emptyList;
while(!oldList.IsEmpty())
newList.Push(oldList.Pop());
现在你所要做的就是实现IsEmpty和Push and Pop,它们是一行或两行。
我在大约二十秒钟内写出来,面试官在这一点上似乎有些困惑。我想他希望我花大约20分钟做大约20秒的工作,这对我来说一直都很奇怪。
答案 1 :(得分:46)
Node p = root, n = null;
while (p != null) {
Node tmp = p.next;
p.next = n;
n = p;
p = tmp;
}
root = n;
答案 2 :(得分:5)
您无需复印。一些伪代码:
prev = null;
current = head;
next = current->next;
(while next != null)
current->next=prev
prev=current
current=next
next=current->next
答案 3 :(得分:4)
多年前,我错过了一个时髦的洛杉矶娱乐公司ASP.NET MVC开发人员的位置,因为我无法回答这个问题:((这是一种淘汰非计算机科学专业的方法。)所以我很尴尬地承认,使用实际的LinkedList<T>
在LINQpad中解决这个问题花了我太长时间:
var linkedList = new LinkedList<int>(new[]{1,2,3,4,5,6,7,8,9,10});
linkedList.Dump("initial state");
var head = linkedList.First;
while (head.Next != null)
{
var next = head.Next;
linkedList.Remove(next);
linkedList.AddFirst(next.Value);
}
linkedList.Dump("final state");
只读LinkedListNode<T>.Next
属性使LinkedList<T>
如此重要。 (鼓励非comp-sci人研究数据结构的历史 - 我们应该问一个问题, linked list 来自哪里---为什么呢存在吗?)
答案 4 :(得分:2)
为什么不只是在尾部有头点,头部有尾点,并通过列表反转prev点的方向?
如果您没有使用头部和尾部,只需通过列出逆转prev关系的列表,然后在您到达时将头部指向具有null prev的那个。
答案 5 :(得分:2)
public Node ReverseList(Node cur, Node prev)
{
if (cur == null) // if list is null
return cur;
Node n = cur.NextNode;
cur.NextNode = prev;
return (n == null) ? cur : ReverseList(n, cur);
}
答案 6 :(得分:1)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReverseLinkedList
{
class Program
{
static void Main(string[] args)
{
Node head = null;
LinkedList.Append(ref head, 25);
LinkedList.Append(ref head, 5);
LinkedList.Append(ref head, 18);
LinkedList.Append(ref head, 7);
Console.WriteLine("Linked list:");
LinkedList.Print(head);
Console.WriteLine();
Console.WriteLine("Reversed Linked list:");
LinkedList.Reverse(ref head);
LinkedList.Print(head);
Console.WriteLine();
Console.WriteLine("Reverse of Reversed Linked list:");
LinkedList.ReverseUsingRecursion(head);
head = LinkedList.newHead;
LinkedList.PrintRecursive(head);
}
public static class LinkedList
{
public static void Append(ref Node head, int data)
{
if (head != null)
{
Node current = head;
while (current.Next != null)
{
current = current.Next;
}
current.Next = new Node();
current.Next.Data = data;
}
else
{
head = new Node();
head.Data = data;
}
}
public static void Print(Node head)
{
if (head == null) return;
Node current = head;
do
{
Console.Write("{0} ", current.Data);
current = current.Next;
} while (current != null);
}
public static void PrintRecursive(Node head)
{
if (head == null)
{
Console.WriteLine();
return;
}
Console.Write("{0} ", head.Data);
PrintRecursive(head.Next);
}
public static void Reverse(ref Node head)
{
if (head == null) return;
Node prev = null, current = head, next = null;
while (current.Next != null)
{
next = current.Next;
current.Next = prev;
prev = current;
current = next;
}
current.Next = prev;
head = current;
}
public static Node newHead;
public static void ReverseUsingRecursion(Node head)
{
if (head == null) return;
if (head.Next == null)
{
newHead = head;
return;
}
ReverseUsingRecursion(head.Next);
head.Next.Next = head;
head.Next = null;
}
}
public class Node
{
public int Data = 0;
public Node Next = null;
}
}
}
答案 7 :(得分:0)
这是一个反转链表的示例代码。
使用System;
class Program
{
static void Main(string[] args)
{
LinkItem item = generateLinkList(5);
printLinkList(item);
Console.WriteLine("Reversing the list ...");
LinkItem newItem = reverseLinkList(item);
printLinkList(newItem);
Console.ReadLine();
}
static public LinkItem generateLinkList(int total)
{
LinkItem item = new LinkItem();
for (int number = total; number >=1; number--)
{
item = new LinkItem
{
name = string.Format("I am the link item number {0}.", number),
next = (number == total) ? null : item
};
}
return item;
}
static public void printLinkList(LinkItem item)
{
while (item != null)
{
Console.WriteLine(item.name);
item = item.next;
}
}
static public LinkItem reverseLinkList(LinkItem item)
{
LinkItem newItem = new LinkItem
{
name = item.name,
next = null
};
while (item.next != null)
{
newItem = new LinkItem
{
name = item.next.name,
next = newItem
};
item = item.next;
}
return newItem;
}
}
class LinkItem
{
public string name;
public LinkItem next;
}
答案 8 :(得分:0)
复杂度O(n + m)。假设head是起始节点:
List<Node>Nodes = new List<Node>();
Node traverse= root;
while(traverse!=null)
{
Nodes.Add(traverse);
traverse = traverse.Next;
}
int i = Nodes.Count - 1;
root = Nodes[i];
for(; i>0; i--)
{
Nodes[i].Next = Nodes[i-1];
}
Nodes[0].Next=null;
答案 9 :(得分:0)
如果需要现成的高效实现,我创建了LinkedList的替代方法,该方法支持枚举和反向操作。 https://github.com/NetFabric/NetFabric.DoubleLinkedList
答案 10 :(得分:0)
这在Leetcode上表现很好。
public ListNode ReverseList(ListNode head) {
ListNode previous = null;
ListNode current = head;
while(current != null) {
ListNode nextTemp = current.next;
current.next = previous;
previous = current;
current = nextTemp;
}
return previous;
}
答案 11 :(得分:0)
public class Node<T>
{
public T Value { get; set; }
public Node<T> Next { get; set; }
}
public static Node<T> Reverse<T>(Node<T> head)
{
Node<T> tail = null;
while(head!=null)
{
var node = new Node<T> { Value = head.Value, Next = tail };
tail = node;
head = head.Next;
}
return tail;
}
答案 12 :(得分:-3)
ref的定义是不必要的,因为如果将节点作为引用类型,则可以这样做:
public static void Reverse(Node root)
此外,面试问题的美妙之处在于减少记忆消耗和反转。也许还会提出一种递归的方式。