PAT-1074. Reversing Linked List

Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.


Input Specification:


Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.


Then N lines follow, each describes a node in the format:


Address Data Next


where Address is the position of the node, Data is an integer, and Next is the position of the next node.


Output Specification:


For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.


Sample Input:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
Sample Output:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

第一次acm题目用java写,不知道class要用main的,,尴尬


下面代码只能通过两个测试例子,思路是首先根据地址查找,找到一个就和排序的位置交换;

然后在reverse,交换

写了两个交换的函数,一个是交换list的内容,一个是交换list中对象的value;前两天刚刚看过一篇文章,大概说java的引用,没有办法通过c++那样改变简单类型参数的值,但是其他对象之类的,传递进来可以类似于引用一样,改变了对象中的值,也就真的改变了,比如代码中的list对象。

只是代码还有待改进。。。


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	
	
	static class MyLinkStruct{
		String thisAddr;
		int value;
		String nextAddr;
		MyLinkStruct(){thisAddr = null; value = 0; nextAddr = null;}
	}
	
	public static void ChangeValue(List list, int pos1, int pos2)
	{
		int tmpValue = list.get(pos1).value;
		list.get(pos1).value = list.get(pos2).value;
		list.get(pos2).value = tmpValue;
	}
	
	public static void ChangeStruct(List list, int pos1, int pos2)
	{
		MyLinkStruct myLs = list.get(pos2);
		list.set(pos2, list.get(pos1));
		list.set(pos1, myLs);
	}
	
	public static void MyPrint(List ls)
	{
		for(int i = 0; i < ls.size(); i ++)
		{
			System.out.print(ls.get(i).thisAddr + " ");
			System.out.print(ls.get(i).value + " ");
			System.out.print(ls.get(i).nextAddr);
			System.out.print("\n");
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String strFirstAddr;
		int  nNum, nReverNum;
		List aryLinkedList = new ArrayList();
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		strFirstAddr = sc.next();
		nNum = sc.nextInt();
		nReverNum = sc.nextInt();
		
		//input list
		for(int i = 0; i < nNum; i ++)
		{
			MyLinkStruct ls = new MyLinkStruct();
			ls.thisAddr = sc.next();
			ls.value    = sc.nextInt();
			ls.nextAddr = sc.next();
			aryLinkedList.add(ls);
		}
		
		//sort by linked rule
		String nNextAddr = strFirstAddr;
		for(int i = 0; i < nNum; i ++)
		{
			for(int j = i; j < nNum; j ++)
			{
				if(aryLinkedList.get(j).thisAddr.equals(nNextAddr) && i != j)
				{
					ChangeStruct(aryLinkedList, i, j);
					break;
				}
			}
			nNextAddr = aryLinkedList.get(i).nextAddr;
		}
		
		//test
		//MyPrint(aryLinkedList);
		
		//reversed
		for(int k = 0; nReverNum != 0 && k < nNum/nReverNum; k ++)
		{
			int i = k*nReverNum;
			int j = (k+1)*nReverNum - 1;
			while(i < j)
			{
				ChangeValue(aryLinkedList, i, j);
				i++;
				j--;
			}
		}
		
		//print
		MyPrint(aryLinkedList);
		
		sc.close();
		
		return ;

	}
	
	

}




你可能感兴趣的:(技术)