链表翻转实现字符串翻转
字符串反转
http://www.nowcoder.com/questionTerminal/e45e078701ab4e4cb49393ae30f1bb04
通过链表翻转对字符串进行反转
import java.util.Scanner; public class Main { public static void main(String[] args) { Main node = new Main(); Scanner sc = new Scanner(System.in); String str = sc.nextLine(); char[] ch = str.toCharArray(); ListNode[] listNodes = new ListNode[ch.length]; for (int i = 0; i < ch.length; i++){ listNodes[i] = new ListNode(ch[i]); } for (int i = 0; i < ch.length - 1; i++){ listNodes[i].next = listNodes[i + 1]; } ListNode res = node.ReverseListNode(listNodes[0]); while (res != null){ System.out.print(res.val); res = res.next; } } public ListNode ReverseListNode(ListNode head){ if (head == null) return null; ListNode pre = null; ListNode next = null; while (head != null){ next = head.next; head.next = pre; pre = head; head = next; } return pre; } } class ListNode{ ListNode next = null; char val; ListNode(char val){ this.val = val; } }