天天看点

面试题16:反转链表反转链表:online judge

反转链表:online judge

题目描述:

输入一个链表,反转链表后,输出链表的所有元素。

(hint : 请务必使用链表)

输入:

输入可能包含多个测试样例,输入以EOF结束。

对于每个测试案例,输入的第一行为一个整数n(0<=n<=1000):代表将要输入的链表的个数。

输入的第二行包含n个整数t(0<=t<=1000000):代表链表元素。

输出:

对应每个测试案例,

以此输出链表反转后的元素,如没有元素则输出NULL。

样例输入:
5
1 2 3 4 5
0      
样例输出:
5 4 3 2 1
NULL      

解题思路:

         这个题目的解题思路在本文中就不重点讲解了,因为变换原理比较简单。本题主要考察的是对特殊情况的处理,例如空指针,链表中节点的个数为一个的时候等。好了废话少说直接贴代码。

java代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
 
public class Main {
    /**
     * Definition for singly-linked list and its operation.
     */
    static private class ListNode {
        public Object val;
        public ListNode next;
         
        /**
         * The constructor of ListNode
         * @param   x: an object to be the value of this ListNode.
         */
        public ListNode(Object x) {
            val = x;
            next = null;
        }
         
        /**
         * @return Returns a string representing the data in this list. 
         */
        public String toString(){
            String result = new String(this.val.toString());
            if (this.next != null)  result = result + " " + this.next.toString();
            return result;
        }
    }
     
    /**
     * Reverse the list in place.
     * @param  head    : the head of to-reverse list.
     * @return ListNode: the head of reversed list.
     */
    private ListNode reverseList(ListNode head) {
        // This is the last node.
        if (head.next == null)      return head;
 
        // Process the remaining node firstly.
        ListNode newHead = reverseList(head.next);
         
        // Reverse current node and its next node.
        head.next.next = head;
        head.next = null;
         
        return newHead;
    }
 
    /**
     * Stub for test.
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(
                                    new InputStreamReader(System.in)));
         
        int size;
        Main reverser = new Main();
        ListNode head = null, current = null;
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            // Start a new test case.
            size = (int) st.nval;
            if (size == 0) {
                System.out.println("NULL");
                continue;
            }
                         
            // Get the test array.
            head = current = null;
            while (size > 0) {
                st.nextToken();
                if (head == null) {
                    current = head = new ListNode((int) st.nval);
                }
                else {
                    current.next = new ListNode((int) st.nval);
                    current = current.next;
                }
                --size;
            }
             
            // Reverse the list.
            head = reverser.reverseList(head);
            System.out.println(head);
        }
    }
 
}
           

C++代码:

#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<memory.h> 
using namespace std;
 
struct node
{       node *next;
        int value;
};
 
 
int main(){
    int num;
    while(cin>>num){
        node ** head=NULL;
        node * tmp=NULL;
         
        if(num==0)
        {
            cout<<"NULL"<<endl;
            continue;
        }
        int x;cin>>x;
        node * pre=new node();
        pre->next=NULL;pre->value=x;
         
        for(int i=1;i<num;i++){
            cin>>x;
            node *now=new node();
            now->next=pre;
            now->value=x;
            pre=now;
        }
        while(1) 
        {
        cout<<(pre->value);
        if(pre->next==NULL)
        {cout<<endl;break;} 
        else cout<<" "; 
        pre=pre->next; 
        } 
    }
     
     
     
    return 0;
}
           

C代码:

#include<stdio.h>
#include<stdlib.h>
struct node 
{
    int data;
    struct node *next;
    struct node *pre;
};
typedef struct node Node;
 
int main()
{
     
    Node *p1,*p2,*head;
    int i,n;
    while(scanf("%d",&n)!=EOF)
    {
        p1=(Node *)malloc(sizeof(Node));
        p1->next=NULL;
        if(n!=0)
        {
            head=p1;
            for(i=0;i<n;i++)
            {
                p2=p1;
                p1=(Node *)malloc(sizeof(Node));
                scanf("%d",&p1->data);
                p2->next=p1;
                p1->pre=p2;
            }
            for(i=0;i<n-1;i++)
            {
                printf("%d ",p1->data);
                p1=p1->pre;
            }
            printf("%d\n",p1->data);
        }
        else printf("NULL\n");
    }
    return 0;
}
           

测试用例:

    功能测试:(输入多个结点的链表,输入只有一个节点的链表) 特殊条件输入:(空指针)

继续阅读