android百科Android开发Android开发经验谈

反转链表

2018-03-25  本文已影响50人  Jimmy5Zhang

1、定义链表结构


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

2、链表反转


    public LinkNode reverseLinkNode(LinkNode pHead) {
        if (pHead == null || pHead.next == null) {
            return pHead;
        }
        LinkNode newNode = pHead;
        LinkNode curr = pHead.next;
        LinkNode temp = null;
        while (curr != null) {
            temp = curr.next;
            curr.next = newNode;
            newNode = curr;
            curr = temp;
        }
        return newNode;
    }

上一篇 下一篇

猜你喜欢

热点阅读