思路: 迭代法,定义三个指针,分别指向前驱,当前结点,后继结点,迭代保存后继结点,修改指向。
public class ListNode {
int val;
ListNode next;
public ListNode(int val,ListNode next) {
this.val = val;
this.next=next;
}
}
public static ListNode ReverseList(ListNode head) {
ListNode pre=null,next;
ListNode cur=head;
while(cur!=null){
next=cur.next;
cur.next=pre;
pre=cur;
cur=next;
}
return pre;
}
思路: 定义两个指针,标记前后位置,修改指向,并迭代整个链表。
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* ReverseList(struct ListNode* Head ) {
struct ListNode *P=NULL;
struct ListNode *M;
while(Head!=NULL){
M=Head;
Head=Head->next;
M->next=P;
P=M;
}
return P;
}



