Skip to content

Latest commit

 

History

History
77 lines (63 loc) · 1.17 KB

File metadata and controls

77 lines (63 loc) · 1.17 KB

链表

反转链表

class ListNode:
    def __init__(self, val=0, nxt=None):
        self.val = val
        self.next = nxt


def reverse_list(head):
    prev = None
    curr = head
    while curr:
        next_node = curr.next
        curr.next = prev
        prev = curr
        curr = next_node
    return prev
package main

type ListNode struct {
    Val  int
    Next *ListNode
}

func reverseList(head *ListNode) *ListNode {
    var prev *ListNode
    curr := head
    for curr != nil {
        next := curr.Next
        curr.Next = prev
        prev = curr
        curr = next
    }
    return prev
}

快慢指针

class ListNode:
    def __init__(self, val=0, nxt=None):
        self.val = val
        self.next = nxt


def half_head(head: ListNode) -> ListNode:
    fast, slow = head, head
    while fast and fast.next:
        fast = fast.next.next
        slow = slow.next
    return slow
package main

type ListNode struct {
    Val  int
    Next *ListNode
}

func halfHead(head *ListNode) *ListNode {
	fast, slow := head, head
	for fast != nil && fast.Next != nil {
		fast = fast.Next.Next
		slow = slow.Next
	}
	return slow
}