题目描述
输入一个链表的头结点,反转该链表并输出反转后链表的头结点。
实验平台:牛客网
解决思路:
java:
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode pNode = head;
ListNode pPrev = null;
ListNode pNest = null;
ListNode pReversedHead = null;
while (pNode != null) {
pNest = pNode.next;
if (pNest == null) {
pReversedHead = pNode;
}
pNode.next = pPrev;
pPrev = pNode;
pNode = pNest;
}
return pReversedHead;
}
}
python:
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
pNode = pHead
pPrev = None
pReversedHead = None
while pNode is not None:
pNext = pNode.next
if pNext is None:
pReversedHead = pNode
pNode.next = pPrev
pPrev = pNode
pNode = pNext
return pReversedHead