题目:输入一个链表,反转链表后,输出新链表的表头。
package case16_ReverseList;
public class ReverseList {
/**
* 翻转链表,需要注意的问题
* 1,空链表
* 2,只有一个节点
* 3,存在多个节点
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(3);
ListNode listNode4 = new ListNode(4);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = null;
ListNode listNodeRev2 = ReverseList2(listNode1);
ListNode l2 = listNodeRev2.next;
System.out.print(l2.val);
}
//反转链表
public ListNode ReverseList2(ListNode head) {
ListNode pre = null;
ListNode now = head;
while (now != null){
ListNode temp = now.next;
now.next = pre;
pre = now;
now = temp;
}
return pre;
}
//构造单链表节点
public static class ListNode {
int val;
ListNode next = null;
public ListNode(int val) {
this.val = val;
}
}
}