LintCode 체인 테이블 뒤집기

제목.

      

  
      1->2->3->null3->2->1->null

제목의 기본 노드 클래스

 public class ListNode {
     int val;
      ListNode next;
     ListNode(int val) {
          this.val = val;
          this.next = null;
      }
  }

코드


public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: The new head of reversed linked list.
     */
    public ListNode reverse(ListNode head) {
        // write your code here
        if(head == null) return head;

        ListNode pre = null;
        ListNode cur = null;
        ListNode next = null;
        cur = head;
        while(cur!=null){
            next = cur.next;
            cur.next =  pre;

            pre = cur;
            cur = next;

        }
        head = pre;

        return head;
    }
}

참조:http://blog.csdn.net/xia744510124/article/details/50038461

좋은 웹페이지 즐겨찾기