zoukankan      html  css  js  c++  java
  • [面试题]给定链表两两倒置?

     

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

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

     1  static LinkedListNode Fun(LinkedListNode root)
     2        {
     3            LinkedListNode top = null;
     4            LinkedListNode parent = null;
     5
     6            while (root != null{
     7                LinkedListNode next = root.Next;
     8                root.Next = null;
     9
    10                if (next == null)
    11                {
    12                    if (parent == null) top = root;
    13                    else parent.Next = root;
    14                    break;
    15                }

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

    25            }

    26
    27            return top;
    28        }
  • 相关阅读:
    [刷题] PTA 7-32 说反话-加强版
    [算法] 堆
    [笔记] 《c++ primer》显示器程序 Chapter7
    [笔记] 《c++ primer》书店程序 Chapter7
    [c++] <vector>
    [笔记] 《c++ primer》书店程序 Chapter2
    [笔记] 《c++ primer》书店程序 Chapter 1
    253. Meeting Rooms II
    461. Hamming Distance
    252. Meeting Rooms
  • 原文地址:https://www.cnblogs.com/sskset/p/748329.html
Copyright © 2011-2022 走看看