[面试题]给定链表两两倒置?

 

将给定链表两两倒置,如 P1 -> P2 -> P3 -> P4 -> P5,转换为 P2 -> P1 -> P4 -> P3 -> P5.

给定链表类 Node{Node* next},写出该转换函数 Node* Fun(Node* line)

 1 None.gif    static  LinkedListNode Fun(LinkedListNode root)
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            LinkedListNode top = null;
 4InBlock.gif            LinkedListNode parent = null;
 5InBlock.gif
 6ExpandedSubBlockStart.gifContractedSubBlock.gif            while (root != nulldot.gif{
 7InBlock.gif                LinkedListNode next = root.Next;
 8InBlock.gif                root.Next = null;
 9InBlock.gif
10InBlock.gif                if (next == null)
11ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
12InBlock.gif                    if (parent == null) top = root;
13InBlock.gif                    else parent.Next = root;
14InBlock.gif                    break;
15ExpandedSubBlockEnd.gif                }

16InBlock.gif                else
17ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
18InBlock.gif                    LinkedListNode nnext = next.Next;
19InBlock.gif                    if (parent == null) top = next;
20InBlock.gif                    else parent.Next = next;
21InBlock.gif                    next.Next = root;
22InBlock.gif                    parent = root;
23InBlock.gif                    root = nnext;
24ExpandedSubBlockEnd.gif                }

25ExpandedSubBlockEnd.gif            }

26InBlock.gif
27InBlock.gif            return top;
28ExpandedBlockEnd.gif        }
posted on 2007-05-16 11:29 K3 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/sskset/archive/2007/05/16/748329.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值