反轉連結清單: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;
}
測試用例:
功能測試:(輸入多個結點的連結清單,輸入隻有一個節點的連結清單) 特殊條件輸入:(空指針)