反转备用元素并附加到列表的末尾

时间:2014-02-07 08:11:20

标签: algorithm pointers language-agnostic linked-list

如果链接列表为a-> x-> b-> y-> c-> z,我们需要反转备用元素并附加到列表的末尾。也就是说,将其输出为a-> b-> c-> z-> y-> x。

我有一个O(n)解决方案,但需要额外的内存,我们分别用2个列表填充备用元素,所以两个列表是abc和xyz然后我们将反转第二个列表并将其附加到第一个尾巴,它变成了abczyx。

我的问题是我们可以这样做吗?或者还有其他算法吗?

5 个答案:

答案 0 :(得分:3)

基本理念:

存储x
a指向b
y指向存储的元素(x) 将b指向c

最后,将奇数位置的最后一个元素指向存储的元素。

伪代码:(简化的列表末尾检查以确保可读性)

current = startOfList
stored = NULL
while !endOfList
  temp = current.next
  current.next = current.next.next
  temp.next = stored
  stored = temp
  current = current.next
current.next = stored

<强>复杂度:

O(n)时间,O(1)空间。

答案 1 :(得分:1)

这是递归模式中的逻辑

public static Node alRev(Node head)
{
    if (head == null) return head;      
    if (head.next != null) 
    {
        if (head.next.next != null) 
        {
            Node n = head.next;
            head.next = head.next.next;
            n.next = null;
            Node temp = alRev(head.next);
            if (temp != null){
                temp.next = n;
                return n;
            }           
        }
        else
            return head.next;
    } 
    else
         return head;
    return null;
}

答案 2 :(得分:0)

这是亚马逊采访中最近的一个问题,这个想法看起来不错,似乎没有任何诀窍。

答案 3 :(得分:0)

带注释的Java代码:

static void change(Node n)
{
    if(n == null)
            return;

    Node current = n;

    Node next = null, prev = null;

    while(current != null && current.next != null)
    {
        // One of the alternate node which is to be reversed.
        Node temp = current.next;

        current.next = temp.next;

        // Reverse the alternate node by changing its next pointer.
        temp.next = next;

        next = temp;

        // This node will be used in the final step
       // outside the loop to attach reversed nodes.
        prev = current;

        current = current.next;            
    }

     // If there are odd number of nodes in the linked list.
     if(current != null)
       prev = current;

     // Attach the reversed list to the unreversed list.   
     prev.next = next;

}

答案 4 :(得分:0)

这里的c代码不使用任何额外的空间来做这件事。享受并享受乐趣 如有任何疑问,请随时提出

    #include<stdio.h>
    #include<stdlib.h>

    int n;
    struct link

    {
    int val;
    struct link *next; 
    };



 void show(struct link *);

 void addatbeg(struct link **p,int num)
 {
 struct link *temp,*help;
 help=*p;
 temp=(struct link *)malloc(sizeof(struct link));
 temp->val=num;
 temp->next=NULL;
   if(help==NULL)
   {
   *p=temp;
   }
   else
   {
    temp->next=help;
    *p=temp;
   }
   n++;
 show(*p);
 }

 void revapp(struct link **p)
 {
 struct link *temp,*help,*q,*r;
 r=NULL;
 temp=*p;
 help=*p;

 while(temp->next!=NULL)
 {

    temp=temp->next;
    q=r;                     //this portion will revrse the even position numbers 
    r=temp;

    temp=temp->next;

    //for making a connection between odd place numbers
    if(help->next->next!=NULL)
    {
    help->next=temp;
    help=help->next;
    r->next=q;

    }

    else
    {
        r->next=q;
        help->next=r;
        show(*p);  
     return;
    }
 }

 }

 void show(struct link *q)
 {
    struct link *temp=q;
   printf("\t");
   while(q!=NULL )
   {

   printf("%d ->",q->val);
   q=q->next;
   if(q==temp)
   {
    printf("NULL\n");
    return;
   }
   }
   printf("NULL\n");
 }

 int main()
 {
 n=0;
 struct link *p;
 p=NULL;

 // you can take user defined input but here i am solving it on predefined list
 addatbeg(&p,8);
 addatbeg(&p,7);
 addatbeg(&p,6);

 addatbeg(&p,5);
 addatbeg(&p,4);

 addatbeg(&p,3);
 addatbeg(&p,2);
 addatbeg(&p,1);


 revapp(&p);

 return 0;
 }`
相关问题