JAVA實現連結串列的反轉(《劍指offer》)
阿新 • • 發佈:2019-02-06
題目描述
輸入一個連結串列,反轉連結串列後,輸出連結串列的所有元素。/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode ReverseList(ListNode head) { if(head==null||head.next==null)return head; ListNode curt = head.next; ListNode newhead= head; ListNode temp = null; newhead.next = null; while(curt!= null) { temp = curt.next; curt.next = newhead; newhead = curt; curt = temp; } return newhead; } }