mirror of
				https://github.com/KieSun/all-of-frontend.git
				synced 2025-05-29 01:49:23 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			27 lines
		
	
	
		
			537 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			27 lines
		
	
	
		
			537 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
// 递归版
 | 
						|
var reverseList = function(head) {
 | 
						|
    const dummy = new ListNode()
 | 
						|
    return reverse(head, dummy).next
 | 
						|
};
 | 
						|
 | 
						|
function reverse (head, dummy) {
 | 
						|
    if (!head) return dummy
 | 
						|
    const tmp = head.next
 | 
						|
    head.next = dummy.next
 | 
						|
    dummy.next = head
 | 
						|
    head = tmp
 | 
						|
    return reverse(head, dummy)
 | 
						|
}
 | 
						|
 | 
						|
// 迭代版
 | 
						|
 | 
						|
var reverseList = function(head) {
 | 
						|
    const dummy = new ListNode()
 | 
						|
    while (head) {
 | 
						|
      const tmp = head.next
 | 
						|
      head.next = dummy.next
 | 
						|
      dummy.next = head
 | 
						|
      head = tmp
 | 
						|
    }
 | 
						|
    return dummy.next
 | 
						|
}; |