我查看了一些代码并发现惯例是将指针类型转换为
SomeStruct*
Run Code Online (Sandbox Code Playgroud)
成
typedef SomeStruct* pSomeStruct;
Run Code Online (Sandbox Code Playgroud)
这有什么好处吗?
我正在准备进行技术面试,我坚持编写这个程序来反转链表的每个k节点.
例如
1->2->3->4->5->6 //Linked List
2->1->4->3->6->5 //Output for k=2
Run Code Online (Sandbox Code Playgroud)
编辑:
这是我的代码.我得到的输出只有6-> 5.
struct node* recrev(struct node* noode,int c)
{
struct node* root=noode,*temp,*final,*prev=NULL;
int count=0;
while(root!=NULL && count<c)
{
count++;
temp=root->link;
root->link=prev;
prev=root;
root=temp;
}
if(temp!=NULL)
noode->link=recrev(temp,c);
else
return prev;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏.谢谢.
编辑:我试图实现Eran Zimmerman的算法如下.
struct node* rev(struct node* root,int c)
{
struct node* first=root,*prev,*remaining=NULL;
int count=0;
while(first!=NULL && count<c)
{
count++;
prev=first->link;
first->link=remaining;
remaining=first;
first=prev;
}
return remaining;
}
struct node* recc(struct node* root,int c)
{
struct node* final,*temp,*n=root,*t;
int count=0; …
Run Code Online (Sandbox Code Playgroud) 当我说不使用任何指针时,我的意思是我们仍然使用“next”指针字段来遍历列表,但在反转链表时不会更改它们。
据我所知,似乎有办法做到这一点:
如果有人能帮助我了解如何使用第一种方法,我将不胜感激。也欢迎其他方法。
任何人都知道如何以相反的顺序打印单链表(一次通过固定且独立于元素RAM的数量).