LC_206. Reverse Linked List

 

https://leetcode.com/problems/reverse-linked-list/description/
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
test case: 3->1->2>5 to 5->2->1->3
x c
 1 public class LC206_ReverseLinkedList {
 2     public ListNode reverseLinkedList(ListNode head){
 3         if (head == null || head.next == null) return head ;
 4         ListNode prev = null, curr= head, next = null ;
 5         /*
 6         * 细节, 如果 写成curr.next != null, curr 会在 NODE4 但是没有进入循环,所以并不会连上前面的点,
 7         * 所以返回的时候会是一个单点,而不是链表
 8         * thats the reason curr should be stopped at null to make sure all prev nodes linked.
 9         * */
10         while (curr != null ){
11             next = curr.next ;
12             curr.next = prev ;
13             prev = curr ;
14             curr = next ;
15         }
16         return prev ;
17     }
18 
19     public ListNode reverseLinkedList2(ListNode head){
20         //base
      if (head == null || head.next == null) return head ;
21 /* 22 * 1-2-3-4 null<-1<-2<-3<-4 23 * current iteration head =2 expect null<-3<-4 24 * 25 * */ 26 ListNode newHead = reverseLinkedList2(head.next) ; 27 ListNode tail = head.next ; //3 28 tail.next = head ; //2<-3<-4 29 head.next = null; //null<-2<-3<-4 30 return newHead ; 31 } 32 }

 

 

 

recursive:   assume Node2 is the head,  subproblem return:

                null<-Node3 <- Node4  

then you want to change to

 

ListNode newHead = reverse(head.next) will generate the following pic: 

ListNode tail = head.next ;  here tail is Node 3

note here since we didnt touch head, thats the reason node2 still points to node3

 

tail.next = head

 

 

head.next = null;   //this is what you need to return for the upper level recursive

 

因为这里是recursive, 一层层走下去 reverse(head.next) 所以base case:

if(head == null || head.next == null) 最后head 会是 原 linkedlist 的尾巴,也就是新头。

 

转载于:https://www.cnblogs.com/davidnyc/p/8456363.html

  • 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
这段代码是一个二叉搜索树(BST)转化为双向链表的函数,同时提供了打印BST和遍历双向链表的函数。 - `def convert_to_doubly_linked_list(self):`:定义一个将BST转化为双向链表的函数。 - `if not self.root:`:如果BST为空,则返回None。 - `def convert(root):`:定义一个递归函数,将BST转化为双向链表。 - `if not root.left and not root.right:`:如果该节点没有左右子树,返回一个只包含该节点值的ListNode。 - `if not root.left:`:如果该节点没有左子树,将右子树转化为双向链表,并将节点值作为新的头结点,返回该节点。 - `if not root.right:`:如果该节点没有右子树,将左子树转化为双向链表,并将节点值作为新的尾结点,返回该节点。 - `left_tail = convert(root.left)`:将左子树转化为双向链表,并返回左子树的尾结点。 - `right_head = convert(root.right)`:将右子树转化为双向链表,并返回右子树的头结点。 - `left_head = left_tail`:将左子树的头结点设置为左子树的尾结点。 - `while left_head.prev:`:找到左子树双向链表的头结点。 - `right_tail = right_head`:将右子树的尾结点设置为右子树的头结点。 - `while 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)`:调用递归函数convert并返回结果。 - `def inorder_traversal(self, root):`:定义一个中序遍历BST的函数。 - `if not root:`:如果该节点为空,则返回。 - `self.inorder_traversal(root.left)`:递归遍历左子树。 - `print(root.val, end=' ')`:输出当前节点的值。 - `self.inorder_traversal(root.right)`:递归遍历右子树。 - `def print_bst(self):`:定义一个打印BST的函数。 - `self.inorder_traversal(self.root)`:调用中序遍历函数遍历BST。 - `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()`:输出一个空行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值