lintcode_99.重排链表

题目

给定一个链表L: L0→L1→…→Ln-1→Ln,
重新排列后为:L0→Ln→L1→Ln-1→L2→Ln-2→…
必须在不改变节点值的情况下进行原地操作。

样例

给出链表 1->2->3->4->null,重新排列后为1->4->2->3->null。

思路

此处使用一个双向链表,在原有的链表上进行操作。依次循环只需把 n 处的节点放到 1 的位置,n-1 处的节点放到 2 的位置,直到输出符合条件的链表。

public class LinkNote {

    public static Note updateLinkNote(Note head, Note tail, int length) {

        if(length <= 0 || null == head){
            return null;
        }else if(null == tail && null != head){
            return head;
        }
        //循环 length/2 次,每次把尾节点放到对应的节点的后面,定义当前要操作的节点
        Note current = null;
        int num = length/2;
        //链表长度为偶数的时候,循环次数 -1
        if(length%2==0 ){
            num = num - 1;
        }
        current = head;

        for (int i=0; i<num; i++){
            //把当前节点的 next 节点保存起来
            Note next = current.next;
            //保存尾节点的前置节点
            Note prev = null;
            if(null != tail){
                prev = tail.prev;
            }
            //当前节点的 next 节点是尾节点
            current.next = tail;
            //尾节点 tail 的 next 节点为原来的 next 节点
            tail.next = next;
            //尾节点 tail 的前置节点为当前节点
            tail.prev = current;
            //下一个要操作的当前节点为原来的 next 节点
            current = next;
            //把原尾节点的前置节点设置为新的尾节点
            tail = prev;

        }

        //设置尾节点的 next 节点为空
        tail.next = null;
        return head;
    }

    public class Note {
        Note prev;
        Note next;
        int value;
        public Note(int vlaue){
            this.value = vlaue;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对下面代码每一步含义进行注释 def convert_to_doubly_linked_list(self): if not self.root: return None def convert(root): if not root.left and not root.right: return ListNode(root.val) if not root.left: right_head = convert(root.right) right_tail = right_head while right_tail.next: right_tail = right_tail.next cur_node = ListNode(root.val, None, right_head) right_head.prev = cur_node return cur_node if not root.right: left_tail = convert(root.left) left_head = left_tail while left_head.prev: left_head = left_head.prev cur_node = ListNode(root.val, left_tail, None) left_tail.next = cur_node return cur_node left_tail = convert(root.left) right_head = convert(root.right) left_head = left_tail while left_head.prev: left_head = left_head.prev right_tail = right_head while right_tail.next: right_tail = right_tail.next cur_node = ListNode(root.val, left_tail, right_head) left_tail.next = cur_node right_head.prev = cur_node return left_head return convert(self.root) def inorder_traversal(self, root): if not root: return self.inorder_traversal(root.left) print(root.val, end=' ') self.inorder_traversal(root.right) def print_bst(self): self.inorder_traversal(self.root) print() def traverse_doubly_linked_list(self, head): cur_node = head while cur_node: print(cur_node.val, end=' ') cur_node = cur_node.next print() def reverse_traverse_doubly_linked_list(self, head): cur_node = head while cur_node.next: cur_node = cur_node.next while cur_node: print(cur_node.val, end=' ') cur_node = cur_node.prev print()
06-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值