题目:翻转链表

翻转一个链表

您在真实的面试中是否遇到过这个题?
Yes
哪家公司问你的这个题? Airbnb Alibaba Amazon Apple Baidu Bloomberg Cisco Dropbox Ebay Facebook Google Hulu Intel Linkedin Microsoft NetEase Nvidia Oracle Pinterest Snapchat Tencent Twitter Uber Xiaomi Yahoo Yelp Zenefits
感谢您的反馈
样例

给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null

挑战

在原地一次翻转完成

标签 Expand
链表



相关题目 Expand

**
* Definition for ListNode.
* 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(null==head||head.next==null) return head;
         ListNode pre = head;
         ListNode next  =  head.next;
         ListNode nextnext = next.next;
         pre.next = null;
         while(next!=null){
              nextnext = next.next;
              next.next = pre;
              pre = next;
              next  = nextnext;
         }
         return pre;
    }
}



你可能感兴趣的:(题目:翻转链表)