美文网首页
LeetCode 206.反转链表

LeetCode 206.反转链表

作者: 不会游泳的金鱼_ | 来源:发表于2019-08-07 17:19 被阅读0次

思路

graph LR
A-->B;
B-->C;
graph LR
C-->B;
B-->A;

依次将指向下一个节点的指针指向上一个节点。

代码

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode temp = head;
        ListNode t2;
        while(temp != null && temp.next != null){
            
            t2 = temp.next;
            temp.next = temp.next.next;
            t2.next = head;
            head = t2;
        }
        return head;
        
    }
}

相关文章

网友评论

      本文标题:LeetCode 206.反转链表

      本文链接:https://www.haomeiwen.com/subject/ildadctx.html